What are the disadvantages of using inheritance as a way of reusing code?

inglor picture inglor · Jul 20, 2010 · Viewed 8.9k times · Source

What are the disadvantages of using inheritance as a way of reusing code?

Answer

IIRC, the Liskov Substitution Principle 1) postulates that one should be able to substitute a class by any of its derived classes; that is, derived classes should not behave radically different than, or violate the contract set up by their base classes.

Obviously then, this principle puts an intentional limit on how a (base) class can be "re-used" by another class (which is derived from it). Other means of using a class, such as aggregation or composition, aren't thus limited by the principle.


1) See e.g. The Liskov Substitution Principle (links to a PDF document).