Head First Design Patterns: Why Composition Should Replace Inheritance by Default
The 'Head First Design Patterns' book advocates preferring composition over inheritance when reusing or combining behaviors in object-oriented design. Inheritance forces subclasses to absorb all parent-class behavior, creating the fragile base class problem where changes to a superclass can unexpectedly break numerous descendants. When behavior varies across multiple independent dimensions, inheritance leads to combinatorial class explosion — for example, two dimensions with two options each already require four concrete classes, and adding a third doubles that count. Composition solves this by delegating behavior to separate, interchangeable objects, so new behavioral variations are added independently rather than multiplied. The principle does not ban inheritance entirely, but reserves it for stable, genuine is-a relationships that respect the Liskov Substitution Principle and remain shallow in hierarchy.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in