Composition vs. Inheritance

Deciding when to use which

Merce Bauza
2 min readJan 29, 2019

Composition and inheritance are two terms used in Object Oriented Programming. Both are design techniques that helps us organise and structure the code in order to optimise the way we write code.

Meaning we can create more easy understandable code, open to extension and closed for modification, good designed and reusable.

Using composition or inheritance could vary depending on the code and what we want to achieve with it.

Understanding composition

To understand composition, we will look at association and aggregation first.

Association represents the relation between two entities. The relation is purely communicative, A and B don’t have to have anything in common, just need to know about each other.

|A|----------->|B|class A
{
private B intanceB;
};

Aggregation defines the typical whole/part relationship. The entity A is the whole and B are parts of A. B could add meaning or more functionality to A.

|A|<>-------->|B|class A
{
private arrayList<B> thingsB;
};

Once we know what association and aggregation are, composition is easy to understand.

Composition has a very similar meaning to aggregation, with the only difference that the ‘part’, in Composition, is controlled by the ‘whole’.

|Bicycle|<#>-------->|Wheel|class Bicycle
{
public Bicycle() {change instanceWheel;}

private Wheel instanceWheel;
};

The difference between aggregation and composition might be difficult to distinguish, but it is implemented very similar in the code.

Understanding Inheritance

Inheritance is a mechanism in which one object acquires all the properties and behaviours of a parent object. The idea behind inheritance is that you can create new entities that are built upon existing ones.

When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

|Bicycle|<>--------<>|MountainBike|class Bicycle
{
public Bicycle() {change instanceWheel;}

private Wheel instanceWheel;
};
class MountainBike extends Bicycle
{
public void setHeight() {};
}

Composition over inheritance

It’s a thing. It has a wikipedia entry and it is an often-stated principle of OOP, included in the book Design Patterns.

But let’s dig into it to see if it’s really the case and why.

Why?

Composition is said to be used over inheritance when using interfaces representing the functionalities of the classes.

Interfaces would be implemented by the classes as need it and in that way, it would allow the use of polymorphism in the code.

Inheritance also uses polymorphism, but only if it doesn’t violate the Liskov Substitution principle, where the subclass needs to be able to implement all the functionalities of the base class, or extend it, if needed.

If you find that your subclass is removing things provided by the superclass, you are violation the LSP and it would be advisable to use composition.

Conclusion

Use inheritance if the subclass it’s the base class with some additives. Any other cases, use composition.

--

--