When to Favor Inheritance Over Composition

In short,

If it is a is-a relationship use inheritance. Use composition when it is a has-a relationship.

Inheritance is perfectly fine as long as nothing changes. But You will face a lot of problems with inheritance when you tend to extend/modify the behaviour of the classes later.

You tend to break your initial assumption and you need to make changes in lot of places. If you use composition you can avoid all these fixing.


If you're working in a language without multiple inheritance, you should always favour composition over inheritance.

In languages without multiple inheritance (Java, C#, Visual Basic.NET), introducing one inheritance hierarchy automatically excludes you from all other, alternative inheritance hierarchies. In these languages, inheritance only locks you in.

With composition, you can do everything you can do with inheritance, as well as some things that you can't do with inheritance, such as, for example, simulating multiple inheritance.

Letting a class implement multiple interfaces essentially simulates multiple inheritance from any client's perspective.

Composing a class with multiple dependencies essentially solves the problem of 'inheriting' from multiple base classes for purposes of reusability.

Inheritance was originally included as a concept in OOP because OOP was originally described in terms of multiple inheritance. See e.g. Object-Oriented Software Construction, which explains OOP in terms of Eiffel - a programming language with multiple inheritance.