Difficulty: Medium
Correct Answer: Data hiding
Explanation:
Introduction / Context:
Object oriented programming encourages developers to protect the internal state of objects from direct external modification. Instead of exposing fields publicly, classes provide methods that control how data is read and updated. This question focuses on the concept that describes preventing client code from directly accessing internal data structures and instead forcing access through a well defined interface.
Given Data / Assumptions:
Concept / Approach:
Data hiding is the specific object oriented principle that focuses on hiding internal representation details from outside code. It is often implemented using access modifiers, such as private fields with public methods. Encapsulation is the broader idea of bundling data and methods together in a class. While closely related, data hiding emphasises restricting direct access, whereas encapsulation emphasises grouping. In many exam contexts, data hiding is the more precise term for protecting internal state from direct client access. Abstraction focuses on exposing essential behaviour while hiding unnecessary detail, and polymorphism deals with many forms of behaviour through a common interface.
Step-by-Step Solution:
Step 1: Identify that the question describes a situation where client code does not directly access internal data.Step 2: Recognise that internal data is hidden and can only be reached through methods defined by the class.Step 3: Connect this behaviour with the principle known as data hiding, which stresses controlling access to internal representation.Step 4: Compare with encapsulation, which is a broader concept of combining data and methods into one unit, not only restricting access.Step 5: Conclude that data hiding is the most accurate name for preventing direct client access to internal data.
Verification / Alternative check:
Textbooks on object oriented design frequently define data hiding as making instance variables private and allowing access only through methods. Many illustrations show that this prevents misuse of the internal state and simplifies future changes, because the implementation can evolve without breaking external code. Encapsulation is usually defined as bundling data and behaviour in one unit, with data hiding presented as a consequence or technique within encapsulation. This supports choosing data hiding for the specific situation described.
Why Other Options Are Wrong:
Option A, encapsulation, is related but focuses on combining data and methods into a single class or module, not necessarily on restricting access. Option C, polymorphism, is about using a common interface to represent different underlying forms or types. Option D, data abstraction, emphasises showing only essential features and hiding complexity, but it is not limited to restricting direct field access. While these ideas are connected, they do not exactly match the description of blocking direct access to internal data.
Common Pitfalls:
A common pitfall is treating all of these concepts as interchangeable. In interviews and exams, you are often asked to distinguish between encapsulation, data hiding, and abstraction. Another mistake is exposing fields as public for convenience, which breaks data hiding and makes future changes risky. Good practice is to keep fields private, design clear methods for interaction, and think carefully about the contract presented to client code, so that the internal representation can change safely when needed.
Final Answer:
Correct answer: Data hiding
Discussion & Comments