The ultimate showdown in Object-Oriented Design. Learn why modern development favors building over inheriting.
Classes derive properties and behaviors from parent classes. Ideally used for strict hierarchical taxonomies (e.g., a "Dog" is an "Animal").
Objects are built by combining smaller, independent components. This is like building with Lego blocks (e.g., a "Car" has an "Engine").
Try to build a Flying & Shooting Robot using both methods.
Select a base class chain. You can only pick one path down the tree.
Click to plug modules into your robot. Mix and match freely!
"text-blue-400">class Robot "text-yellow-600">{
"text-yellow-400">constructor"text-gray-400">("text-gray-400">) "text-yellow-600">{ "text-red-300">this.energy = 100; "text-yellow-600">}
"text-yellow-600">}
| Feature | Inheritance | Composition |
|---|---|---|
| Relationship | IS-A | HAS-A |
| Flexibility | Low. Defined at compile/creation time. | High. Can change at runtime. |
| Coupling | Tight. Child depends on Parent details. | Loose. Components are independent. |
| Problem | Fragile Base Class, Explosion of subclasses. | More boilerplate code, harder to debug. |
You are building a UI Library. You have a `Button`. You want a `Button` that has an Icon, and a `Button` that has a Loading Spinner, and a `Button` that has both.