Q: How does encapsulation improve maintainability in OOP?
-
A
By exposing all internal data to the user.
-
B
By bundling data and methods together and restricting access to certain components.
-
C
By making all methods static.
-
D
By avoiding the use of classes altogether.
B
Answer:
B
Explanation:
Encapsulation improves maintainability in object-oriented programming by keeping the internal state of an object hidden from the outside world and only exposing a controlled interface for interaction. This is achieved by bundling the data (attributes) and methods (functions) that operate on the data into a single unit called a class, and by using access modifiers (private, public, protected) to restrict access to the class's components. This approach ensures that the internal representation of an object can be changed without affecting the external code that interacts with the object. It reduces dependencies and makes the code more modular, which in turn makes it easier to update, debug, and maintain. Encapsulation also promotes the principle of information hiding, which helps prevent accidental or unauthorized modifications to the object's state, further enhancing the stability and reliability of the software.
Related Topic:
Share Above MCQ