Inheritance vs Composition

Introduction

Inheritance and composition are among the fundamental attributes of OOP that make the design of software easier and cleaner. They are greatly renowned for the way they enable the reusability of codes in OOP design.

In software design, there are principles that determine what a good software codebase looks like and Object-Oriented Design is a big player to achieve these principles.

With help of inheritance and composition, we are able to write cohesive and loosely coupled software that is simple to understand and easier to modify which are the characteristics of good software.

What is what?

Inheritance Is a mechanism of deriving a new class from an existing one, it brings an IS-A relationship between the involved classes. This relationship means that the derived (child) class will inherit all the implementation of the base (parent) class.

Example:

The above example shows how really a derived Painting class is a special kind of base Picture class with a few additions that are unique to the painting.

Composition is a mechanism of building the functionality of a component by assembling implementations from other components or objects. It brings a HAS-A relationship between involved objects or components. In composition an object or a class uses only the implementation from other objects on a need basis, simply borrows the implementation that is essential for its function.

Example

The above examples clearly shows how you can create a Band class by assembling functionalities from different individual objects. In terms of relationship this indicates that a band class composed of (HAS-A relationship) drummer, guitarist, vocalist who themselves are there own classes.

Comparisons

They are both fundamentals in OOD because they bring the reusability of codes to life. Even though they achieve this in different ways which bring lots of arguments about which is the better approach that we should use. In the book Design patterns in Ruby by Russ Olsen, he advises to prefer composition over inheritance which really means that composition should be our goto in terms of introducing relationships in software, but what I think is more important is understanding the context in which to use any of these approaches.

When is inheritance more important?

When you are sure that the relationship between the base and parent class is of an IS-A relationship, which is the common mistake that we make when introducing inheritance between classes because sometimes it might be computationally heavy to inherit from a parent class and end up omitting behaviors that are not of the derived class. Just like in construction, the base class should be a building block of the derived class and that happens only when the derived class is additive to what it has inherited from the base class.

When is composition more important?

Composition is more important when the above is not the case, i.e if you need to assemble a component from different objects but ensure that your codebase is as cohesive and decoupled as much as possible then composition, is your goto. There is also another case when the is IS-A (specialization) relationship doesn’t really mean that you should prefer inheritance when but factor in the time in the future that you predict that the derived class is going to grow much bigger than the base class this is also a good indicator that you should use composition from the beginning. Kinda like building a five-story house on a supposed foundation of a two-story building.

Previous
Previous

Software Testing: Essential Concepts and Practices

Next
Next

DEPENDENCY INJECTION IN TESTING