Quick Index
Overview


We prefer composition over inheritance. In other words, we attempt to gain code reuse by composition first.

Explanation


Team
Here we show a Team object with two components (ivars).

Let's compare composition vs inheritance by using their identity tests ("has-a" and "is-a")"

  • Team is-a coach inheritance
  • Team has-a coach composition

  • Team is-a list of players inheritance
  • Team has-a list of players composition

In both cases, the "has-a" (composition/components) relationship makes more sense than the "is-a" relationship.

We will firstly look for reuse by composition, and secondly reuse by inheritance.


References