3

I have been looking for an answer about what could be a simple code example of the composition. I know the differences of composition and inheritance, but I am not able to figure it out using code.

Any comparison based example(using both approaches) about why I should use composition will be very helpful.

Thanks in advance

Tech Jay
  • 404
  • 1
  • 6
  • 16
  • I think I have got the answers from the links below. Basically my concern was regarding how the concept of changing the base class method can affect on whether we should have used the inheritance or composition. – Tech Jay Oct 01 '13 at 16:00
  • And i think that if we use inheritance and in case any change is made in the base class function, then the actual calling client code will be affected. But had we used composition, changing the base class function would require us to change the class using it. Client code would have remained the same. Another point is we should use inheritance only if we are required to use the entire functionality of the base class. I will repeat again that my main concern of starting this thread again was the point related to change in base class functionality. – Tech Jay Oct 01 '13 at 16:15

3 Answers3

1

Fundamental difference is that inheritance fixes the class hierarchy at compile time, while composition allows to decide the composed class relationship at runtime.

With inheritance, the code it fixed with the inheritance hierarchy. For example, If We inherit Circle class from Shape class, Shape is fixed as base class for Circle class.

Suppose, we further classify Shape as 2DShape, and 3DShape. Now with inheritance it is not possible for Circle class to inherit from 2DShape.

With Composition it is possible. So you can change the composed class to any of the derived type of composed class at runtime.

Tilak
  • 30,108
  • 19
  • 83
  • 131
1

You may probably want to check Composition over inheritance

To favour composition over inheritance is a design principle that gives the design higher flexibility, giving business-domain classes and more stable business domain in the long term. In other words, HAS-A can be better than an IS-A relationship

Check the answer of aleemb in a related post in which he explains the difference with code example

Here is a useful link which explains why prefer composition over inheritance

Though both Composition and Inheritance allows you to reuse code, one of the disadvantage of Inheritance is that it breaks encapsulation. If sub class is depending on super class behavior for its operation, it suddenly becomes fragile. When behavior of super class changes, functionality in sub class may get broken, without any change on its part. One example of inheritance making code fragile is method add() and addAll() from HashSet. Suppose, If addAll() of HashSet is implemented by calling add() method and you write a sub class of HashSet, which encrypt the content before inserting into HashSet. Since there are only one methods add(), which can insert object into HashSet you override these method and called your encrypt() method by overriding add(). This automatically covers addAll() as well, because addAll() is implemented using add(), it looks very enticing.If you look closely you will see that this implementation is fragile, because its relied on super class behavior. If base class wants to improve performance and implements addAll() without calling add() method, below example will break.

public class EncryptedHashSet extends HashSet{
.....

public boolean add(Object o) {
   return super.add(encrypt(o));
}

}

If you have used Composition in favor of Inheritance you won't face this problem and your Class would have been more robust, because you are not relying on super class behavior any more. Instead you are using super class method for addition part and you will benefit with any improvement in addAll() as shown in below example:

public class EncryptedHashSet implements Set{
private HashSet container;

public boolean add(Object o) {
   return container.add(encrypt(o));
}

public boolean addAll(Collection c) {
   return conatainer.add(encrypt(c));
}

.......
}
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

I think Joshua Bloch says it best: Item 16: Favor composition over inheritance

bcorso
  • 45,608
  • 10
  • 63
  • 75
  • All of the answers posted above by you guys were really helpful. But somehow, i am not able to mark all of them answers. Still thanks for the replies guys, they were really helpful. One more thing, i would love you guys to check out my reply, POSTED JUST AFTER MY QUESTION above, and let me know if my understanding is correct or not. – Tech Jay Oct 01 '13 at 16:22