Prefer composition over inheritance

posted 29 Dec 2010, 09:26 by Sanjeev Kumar   [ updated 11 Nov 2011, 06:09 ]
Trust me on this one, I have learnt it the hard way. To start with lets start with the understanding of the terms composition and inheritance
  • Composition
    • Functionality of an object is made up of an aggregate of different classes.  
    • In practice, this means holding a pointer to another class to which work is deferred.
    • Collaboration is implemented simply by forwarding all calls to an object field
    • it has no dependence on implementation details of the object field
    • it is more flexible, since it is defined dynamically at run-time, not statically at compile-time
  • Inheritance
    • Functionality of an object is made up of it's own functionality plus functionality from its parent classes.
Sub-classing or inheritance has the following issues :
  • it violates encapsulation, since the implementations of the superclass and subclass become tightly coupled
  • new methods added to the superclass can break the subclass
  • superclass and subclass need to evolve in parallel
  • designing a class so that it may be safely extended takes extra work -  extending a class not explicitly designed for such use is risky
  • different packages are often under the control of different programmers - extending a class in a different package is risky
A common exception is the template method design pattern. There, the safest style is to make all items in the abstract base class final, except for the single abstract method which needs to be implemented by the subclass.