Composition VS Inheritance

When programming in OOP (Object Oriented Programming), one of the most used and popular practice is to use Inheritance in most languages. Although sometimes it is not looked at as good practice it is still popular. Some new languages such as Go do not use inheritance in their programming only the alternative to inheritance which is composition.

What are they?

So what are composition and inheritance? Below you will find WikipediasWikipedias definition of them both.

Composition: in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.

Inheritance: the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation. Also defined as deriving new classes (sub classes) from existing ones such as super class or base class and then forming them into a hierarchy of classes. In most class-based object-oriented languages, an object created through inheritance, a “child object”, acquires all the properties and behaviors of the “parent object”.

So which is better?

The biggest difference between the two is the way the objects relate to each other. For example in inheritance, the bus is the vehicle and in composition, the bus has a steering wheel. With inheritance, you have to be careful when changing the base class because it can cause some problems in your code. With composition, you have more flexibility when changing parts in your base code also can allow you to change behavior and runtime. Composition makes it easier to combine components because there is more commonality between the class tree.

This approach accommodates future requirement changes, that may require a complete restructuring of the class tree in the inheritance approach, more easily. We can simply add a new component to the composited class rather than modifying the superclass to adapt to changes.

They also have different purposes, inheritance is used to design a class on what it is, composition to design a class on what it does. They can usually be used interchangeably, inheritance uses a well-known method of polymorphism. They do not make composition to work with polymorphism, although it can be done.

In most cases, composition can be used interchangeably with inheritance. One thing that makes inheritance so well-known is polymorphism. Composition is initially not designed for polymorphism. But most programming languages allow us to do that with interfaces (known as protocols in the Swift world).

Conclusion

Like everything with software development, we look at use case. Everything comes with pros and cons, most the time composition can substitue inheritance but it doesn’t always work. In most cases ask yourself would it make more sences to use composition and as long as your not making specialized sub or superclasses if the answer is no then you should use inheritance.