Don’t Pay Inheritance Tax (in programming!)

Inheritance is a fast and loose way of implementing functionality based on the properties of something else: a car is a type of vehicle, and vehicles can move, have a speed, and have a location. Rather than hardcoding a car, just initialise it as a type of vehicle.

Simple.

Inheritance couples components: the child class is coupled to all of its ancestor classes, and code that uses the child class is also similarly coupled. But earlier in the book, the dangers of coupling components in code were strongly shown.

Could there be a better way? According to the book, there is. Interfacing:

“Most OO languages allow you to specify that a class implements one or more sets of behaviors. You could say, for example, that a Car class implements the Drivable behavior and the Locatable behavior.

These declarations create no code: they simply say that any class that implements Drivable must implement the two methods getSpeed and stop, and a class that’s Locatable must implement getLocation and locationIsValid. This means that our previous class definition of Car will only be valid if it includes all four of these methods.”

Don’t pay programmatic inheritance tax. Interface instead.

Thank you for reading, see you tomorrow!
#PathToSWE