Stage 1 · Lesson 6 of 8
Composition over inheritance
Composition means building a class by holding other objects and passing work to them, a relationship, instead of extending a parent. The book, which catalogued the classic , put it as "favour object composition over class ".
A Stack that has a list exposes only push and pop. A Notifier that has a list of channels can send by email and SMS without a class for every combination. Parts can be swapped at run time, tested on their own and reused in other classes. Most of the design patterns later in this roadmap are composition in a named shape.
Common trapturning the rule into "never inherit": a shallow hierarchy for a true is-a, such as UI widgets in a framework, is still fine.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking the Object Oriented Design Interview
Design Gurus · Intermediate · Subscription
Its opening chapters teach the four OOP ideas applied inside real designs, with UML and running code
- CourseFreeMIT 6.102: Software Construction
MIT · Intermediate
Free MIT readings: Abstract Data Types, Rep Invariants and Interfaces and Subtyping are this stage at university depth