Inheritance and Composition

Henrique Siebert Domareski
3 min readJun 16, 2020

--

Inheritance and Composition are two ways to create a relationship between classes and objects in Object-Oriented Programming Languages. In this article we are going to understand how we can use it and when we should use Inheritance or Composition

Inheritance

Inheritance it’s a principle of the Object-oriented programming that allow the classes to share attributes and methods beyond ‘Inheritance’. This is useful to reuse code or behaviour. In .NET it’s not allowed to have multiple inheritance, so you can only inheritance from one class.

This is a definition of Inheritance from the Microsoft documentation:

Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class that reuses (inherits), extends, or modifies the behavior of a parent class

In Inheritance, we have two types of classes, the Base class and the Derived class. The Base class is the class that give the characteristics to the other class. The Derived class is the class that inherited the characteristics from the base class.

Let’s see an example of Inheritance in a real-world scenario using C#. I have this class that is named as Entity. This is a generic class that has two properties, an Id and the date on which the entity was created. Every class that inherited from Entity, will also have access to this two properties. This is an abstract class because this class cannot be instantiated, it only can be inherited.

Now let’s create a new class that will inheritance from the Entity class. To use inheritance in .NET we use the two points following by the name of the class that we want to inheritance:

Book : Entity

In this case:

  • The Book class is the derived class.
  • The Entity class is the base class.

This is the Book class that inherited from Entity:

Now in the Book class you can access the properties Id and CreatedDate through inheritance:

var book = new Book
{
Id = 1,
Name = "Clean Code",
Author = "Robert C. Martin",
CreatedDate = DateTime.Now
};

It’s good to keep in mind that Inheritance can causes high coupled between your classes. For example, if you change something in the base class, it will directly affect all the derived class.

So always when you think if you should use or not use Inheritance, you can ask for yourself if the class ‘is a’ of something. For example, Book is an Entity, so in these cases it’s ok to use inheritance. But if we have another class, the Category class for example, we cannot say that Book is a Category, so in this case the Book class should not inheritance from Category, instead of that, if we want to created a relationship between this two classes, we should use composition.

Composition

Composition is a way to create a relationship between classes, and it’s better than Inheritance in almost all cases, because composition doesn’t make your code high coupled. This is a definition of Composition from the book ‘Software Architect’s Handbook’:

Composition is a relationship in which an object cannot exist independently of another object.

To use composition, you can ask for yourself if the class ‘has-a’ of something. For example, the class Book ‘has-a’ Category. Note that in this case, we cannot say that Book is a Category, but we can say that Book has a Category, so in this case we should use Composition (see the line 10):

Now in the Book class you have access to the properties from the Category class through Composition:

var book = new Book
{
Name = "Clean Code",
Author = "Robert C. Martin",
Category = new Category() {
Description = "Computers & Technology"
}
};

Conclusion

In almost all cases it’s better to use Composition instead of Inheritance because then you will make your code less coupled. But this does not mean that you never should use Inheritance, there are some cases that will make more sense to use Inheritance than Composition. Always remember about the ‘is-a’ and the ‘has-a’ rule, because this will help you to decide if you should use Inheritance or Composition.

Thanks for reading!

References

Microsoft documentation

Software Architect’s Handbook — Joseph Ingeno

--

--

No responses yet