Advantages of encapsulation:
It improves maintainability and flexibility and re-usability: .... Since the implementation is purely hidden for outside classes ...
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Dan Drillich wrote:Hey Henno,
Encapsulation in Java with example says -
Advantages of encapsulation:
It improves maintainability and flexibility and re-usability: .... Since the implementation is purely hidden for outside classes ...
Regards,
Dan
... Hence the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-usability of the underlying class.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Henno Vermeulen wrote:Still I feel that the statement "Encapsulation makes it easier to reuse classes" isn't true when you are given a class that never changes.
Henno Vermeulen wrote:Because the class is poorly encapsulated you can actually do more with it, so it should in fact be easier to reuse!
Joe Bishara wrote:This sometimes involves reusing classes i.e. swapping one class for another class (an improved version).
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:Reuse implies using something that is meant to do one task, to do another.
Henno Vermeulen wrote:I don't really see how it becomes harder to reuse a class when it has poor encapsulation.
Henno Vermeulen wrote:
...
It is true that a poorly encapsulated class can be harder to reuse when it has certain invariants between it's fields that need to hold. Exposing the fields directly enforces the client of the class - instead of the code of the class itself - to ensure that these invariants are not violated. This is an extra burden on the client of the class, so indeed it is harder to reuse!
Would this be their reasoning or am I missing a simple point...?
Henno Vermeulen wrote:Still I feel that the statement "Encapsulation makes it easier to reuse classes" isn't true when you are given a class that never changes. The question never mentions maintainability, hence my confusion!
Perhaps it should have read something like "Encapsulation makes it easier to reuse classes that require maintenance" or "Encapsulation makes it easier to maintain reusable classes".
Do you think that this was the reasoning of the authors of the question, or is there another mechanism in which encapsulation helps improve reusability?
Henno Vermeulen wrote:I am actually suggesting this may be the answer in my original post. Still I'm not sure if this is really the reason.
Henno Vermeulen wrote:Somehow I'm still left with a gut feeling that the question is debatable
Consider Paul's rocket mass heater. |