Advantages of Composition Over Inheritance
Disadvantages of Inheritance:
- You can’t change the implementation inherited from super classes at runtime (obviously because inheritance is defined at compile time).
- Inheritance exposes a subclass to details of its parent class implementation, that’s why it’s often said that inheritance breaks encapsulation, like the design of protected modifier.
- The tight coupling provided by inheritance makes the implementation of a subclass very bound up with the implementation of a super class that any change in the parent implementation will force the sub class to change.
- Excessive reusing by sub-classing can make the inheritance stack very deep and very confusing too.
On the other hand Object composition is defined at runtime through objects acquiring references to other objects. In such a case these objects will never be able to reach each-other’s protected data (no encapsulation break) and will be forced to respect each other’s interface. And also, implementation dependencies will be a lot less than in case of inheritance.