Difficulty: Easy
Correct Answer: Encapsulation
Explanation:
Introduction / Context:
Object-oriented programming is built on several core principles, including encapsulation, inheritance, and polymorphism. Encapsulation describes the bundling of data and the operations that work on that data into a single unit, typically a class. Interviewers frequently ask this definition style question to make sure you can correctly match the concept name to its description.
Given Data / Assumptions:
Concept / Approach:
Encapsulation is the object-oriented concept that groups related data and behavior into a single class or object. By encapsulating fields and methods together, the class becomes a self-contained unit that manages its own state. Information hiding is closely related but focuses specifically on restricting access to internal details via access modifiers. Polymorphism and inheritance are different concepts dealing with substitutability and reuse through class hierarchies. Since the question specifically mentions keeping actions and attributes together under a single unit, encapsulation is the correct term.
Step-by-Step Solution:
Step 1: Identify that the phrase "keeping the actions and attributes together" refers to combining data and behavior.
Step 2: Recognize that in object-oriented programming, a class achieves this by holding fields and the methods that work on them.
Step 3: Recall that encapsulation is defined as the bundling of data and methods into a single unit.
Step 4: Distinguish encapsulation from information hiding, which is about hiding internal implementation details from outside code.
Step 5: Conclude that encapsulation is the best match to the description in the question.
Verification / Alternative check:
Textbooks and tutorials on object-oriented design commonly define encapsulation as combining data and behavior into one unit and often show examples of classes that contain both fields and methods. They frequently explain that encapsulation enables information hiding, but the core definition matches the question wording exactly. By contrast, polymorphism is defined as the ability of different types to be treated through a common interface, and inheritance as deriving new classes from existing ones, which do not match the given description.
Why Other Options Are Wrong:
Option B, information hiding, is related but emphasizes restricting external access to internal details, typically through private fields and public methods. The question does not mention hiding or access control, only bundling. Option C, polymorphism, concerns the ability of different classes to respond to the same method call in different ways. Option D, inheritance, is about forming parent child relationships between classes to reuse code, not about bundling data and behavior in a single unit.
Common Pitfalls:
A frequent confusion is treating encapsulation and information hiding as identical. While they are closely linked, encapsulation is the broader concept of bundling, and information hiding is one of the benefits that encapsulation enables. Another pitfall is mixing up definitions of the various object-oriented principles in interviews; practicing clear, concise definitions helps avoid such confusion.
Final Answer:
Keeping the actions and attributes together under a single unit in object-oriented design is called encapsulation.
Discussion & Comments