1

I have seen many articles where it says inheritance breaks encapsulation

http://igstan.ro/posts/2011-09-09-how-inheritance-violates-encapsulation.html

But I am unable to understand the concept behind it. in the given example

Can anyone please explain how composition is avoiding this problem

1
  • The wrapped HashSet calls its own add and not the wrapper's add. Commented Oct 29, 2016 at 16:15

2 Answers 2

1

In the example in the article you linked to:

     set.addAll(Arrays.asList("Snap", "Crackle", "Pop"));

This calls InstrumentedHashSet.addAll.

InstrumentedHashSet.addAlladds 3 to the counter, then callsHashSet.addAll`.

HashSet.addAll calls this.add three times to add each of the elements.

But this.add is really a call to InstrumentedHashSet.add !

Each call to InstrumentedHashSet.add adds 1 to the counter and calls super.add.

Each HashSet.add call does the actual work of adding the element to the set.

The end result is that we have added 6 to the counter, not 3 as you would expect.

In order for InstrumentedHashSet to be implemented correctly, it needs to know how HashSet.addAll is implemented, and NOT increment the counter in addAll. But that knowledge breaks encapsulation. A subclass should not need to know how its superclass is implemented.


Can anyone please explain how composition is avoiding this problem

It avoids it because when HashSet.addAll calls this.add, those calls to this.add are not calls to InstrumentedHashSet.add. They are direct calls to HashSet.add.

In fact, provided that HashSet.addAll implements the Set.addAll contract, it doesn't matter to InstrumentedHashSet how it its implemented. No breaking of encapsulation here.

1

I have seen many articles where it says inheritance breaks encapsulation

http://igstan.ro/posts/2011-09-09-how-inheritance-violates-encapsulation.html

But I am unable to understand the concept behind it. in the given example

Can anyone please explain how composition is avoiding this problem

The word "break" might be too strong, but inheritance certainly does at least compromise encapsulation.

An important goal of encapsulation is to separate the implementation of an API from its contract. In other words, clients that wish to use an API should need only understand its "rules of the road" (i.e. its contract) without needing to know anything about how it works internally. This allows modules of large systems to become decoupled and to evolve independently (as long as the contract remains intact).

Unfortunately, when one class inherits from another, the subclass often becomes dependent upon at least some of the implementation details of the base class ... which violates the principle of encapsulation.

There are two common ways in which this can occur:

  • If a subclass uses a method inherited from the base class -and- that method invokes another method which is part of the exported API (so called "self use" of API methods). In order to avoid errors, the author of the subclass needs to know about the self use of methods by the base class. This is an implementation detail that a client of an API would otherwise not need to know about.

  • Protected members. Sometimes a subclass cannot implement a function without access to data or behavior from the parent class. Java provides the protected access type to enable access to members by subclasses. In this way, the subclass is very directly accessing information and methods that would otherwise be encapsulated within the parent class. Thus, the subclass becomes dependent upon the implementation details of the parent.

The downside of having implementation details leak out of your class is that the correct use of it no longer just depends on the API contract. Whenever the implementation changes, there is a chance that subclasses will break.

2
  • +1. I disagree with you a bit on the terminology, though: I'd argue that if correct subclassing depends on a certain behavior, then really that behavior is actually part of the class's contract, and is not merely an implementation detail. But it's hard to get this right; it's much easier (and more natural) to define the contract for clients of a class than for its subclasses.
    – ruakh
    Commented Oct 29, 2016 at 18:00
  • " I'd argue that if correct subclassing depends on a certain behavior, then really that behavior is actually part of the class's contract," ... no argument. The key point here is that whether it was intended or not, the behavior becomes part of the exported API.
    – scottb
    Commented Oct 29, 2016 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.