Skip to main content
PProfileMatch
← Path

Object-Oriented Programming · Inheritance vs Composition in OOP: Which to Use and Why

0 XP
  1. 1 current stop: learning
  2. 2 upcoming
  3. 3 upcoming
  4. 4 upcoming
  5. 5 upcoming

Finish after stop 5

Stop 1 of 5 · Gadget assembly line

Trick to keep in mind+3 XP

Inheritance multiplies classes, composition adds them.

With subclasses, each combination of features needs its own class: two payment methods with three optional yes/no extras is 2 x 8 = 16 classes. With composition each extra is a small wrapper written once and snapped on at run time, so the same variety needs about 2 + 3 = 5 classes. Independent features are the warning sign.

Read it once, then a question tests whether the trick stuck.