Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use composition? [duplicate]

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

like image 294
Tech Jay Avatar asked Feb 20 '26 12:02

Tech Jay


2 Answers

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));
}

.......
}
like image 130
Rahul Tripathi Avatar answered Feb 23 '26 20:02

Rahul Tripathi


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.

like image 26
Tilak Avatar answered Feb 23 '26 21:02

Tilak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!