Composition Over Inheritance
ELI5 — The Vibe Check
Composition Over Inheritance means building objects by combining small, focused pieces instead of creating deep inheritance trees. Instead of 'Dog extends Animal extends LivingThing,' you compose: Dog has a Walker, a Breather, and a Barker. It's more flexible, less fragile, and easier to change.
Real Talk
Composition over inheritance is a design principle favoring object composition (has-a) over class inheritance (is-a) for code reuse. Composition provides greater flexibility, avoids fragile base class problems, and supports runtime behavior changes. Aligns with SOLID principles, particularly the Open/Closed and Liskov Substitution principles.
When You'll Hear This
"We refactored the class hierarchy to use composition — now adding features doesn't require modifying base classes." / "React moved from class inheritance to composition with hooks — same principle."
Related Terms
SOLID Principles
SOLID is five principles that keep object-oriented code from turning into spaghetti: Single Responsibility, Open-Closed, Liskov Substitution, Interface Seg
Strategy Pattern
You're writing a sorter and want to sort by price, name, or date depending on user choice.