Composition vs Inheritance

The Basketball Player

Imagine if I told you that you can’t throw a basketball into a hoop, because you’re not 7 feet tall. Doesn’t seem fair right?

What if I gave you a compromise, and told you that you can throw that basketball, but only if you make sure that you tell everyone that you’re definitely not 7 feet tall – is that more acceptable?

As contrived as this scenario is, that is one of the biggest downfalls of using an inheritance model: Trying to define something based on what it is, rather than what it can do requires that exceptions to the rules are explicitly stated, and can lead to undesired behaviour – what if there’s something about a basketball player which actually requires them to be 7 feet tall, and fails otherwise? Further reading