Inheritance and composition are two choices we have in the case of object-oriented programming practices to follow to have code reusability in place. The concept of code inheritance comes into the picture when you want to inherit some properties and functions from the base class and have some additional properties and functions in the derived class.

Below are the broader types of Inheritance supported in any object-oriented languages. I am taking c# as an example below.

Note. Multiple inheritance (One class inheriting from two or more classes) was supported in C++, but it's not supported in advance OOP languages like C# and Java.

Inheritance

Composition is another way to support code reusability. Here, as the name suggested, one class is composed of another class. For example, let's say class B needs some capabilities of class A. In that case, instead of inheriting from class A, it will have a reference of class A added to class B.

This can be achieved by declaring a class A variable inside class B, initializing it in the constructor, and only using needed properties and functions inside it.

Composition

Output

Output

I have explained the concept of inheritance and composition in simple words with examples. Now, let’s discuss the pros and cons of using them over each other.

Evaluate your needs and always prefer using composition over inheritance because of the drawbacks inheritance can introduce in the long term in your enterprise application.

For example, Instead of having Animal as a base class and Cat, Dog, Tiger, or Human inheriting from it, it's better to create a small interface based on capabilities like IBarkable, IMeowable, ITalkable inheriting from ICommunicate and use ICommunicate in composition and runtime passes the needed reference of the concrete class implementing the relevant interface.

Use inheritance in case of a perfect is-a relationship. If you need all the functionalities in a base class and you just want to extend it a bit, and if it's a single inheritance, one base class, and one derived class for special purpose needs, then it's fine. If a derived class only needs a few things in the base class but not all the things, then rethink using inheritance. Instead of that, you can prefer taking that capability out as an interface and then using composition instead of inheritance.