Inheritance vs. Composition

Over-used Inheritance->Justified? OR Composition->Better?

Inheritance and Composition

Both are fundamental in OO design. Composition is fairly easy to understand, e.g., a chair has legs, a face has nose, eyes, ears, etc. The definition of Inheritance is more 'abstract'.


Inheritance -> think of hierarchy. From programming perspective, extend/override existing functionality,

Composition -> think of parts and components (composed of). From programming perspective, 'holding a reference to an object'.


In the following, there are a few pointers on when to use Inheritance, and when to use Composition. Agreed/Disagreed?


Inheritance

-> Both classes are in the same "logical" domain

-> Additive functionalities are only required by the subclass


Composition

-> Classes are not inside same ''logical'' domain

-> When the hierarchy is too long. Transitive property of Inheritance requires all of the superclasses to be instantiated before being able to 'test' the inherited class. Very difficult from testing perspective.