Section 1/4has-a vs is-a
has-a vs is-a
A common OOP mistake is using inheritance when composition is correct. A Car should not inherit from Engine.
Composition
Composition is a "has-a" relationship where one object ownsanother. The contained object is created inside the container — and if the container is destroyed, the contained object is too. This is called a strong has-arelationship.
has-a vs is-a
- is-a → use inheritance: a Dog is an Animal
- has-a → use composition: a Car has an Engine
A common OOP mistake is using inheritance when composition is correct. A Car should not inherit from Engine.