Inheritance over composition

twitter logo github logo ・1 min read

More often than not, I hear statements like „favor composition over inheritance“. I think there are valid uses of inheritance, so once again, it depends on context (let‘s ignore deep inheritance hierarchies, I hope we all agree that is usually a bad idea).

What are your experiences? Have you experienced situations where inheritance was really useful?

twitter logo DISCUSS (7)
markdown guide

Inheritance for me seems to work best when the inheritance relationship exists in the domain problem I'm modelling.

A triangle is a polygon, etc.

If my main goal is code re-use I always favor composition, and believe that abstractions should depend on details, not vice versa (as is the case with inheritance, details depending on abstractions)


Do you use dependency inversion/injection in the composition case? Otherwise depending on the details seems rather unusual to me :)


Dependency inversion - YES! classes should be honest and declare in the constructor what they need to function correctly.

Dependency injection - If the app is large enough that it make sense to use a DI container, sure. Almost all of what I write these days is either Angular or ASP.NET Core web applications, so Dependency injection is pretty much a guarantee.


The most recent case where I was happily using inheritance was for a data transfer object (DTO) where a variety of messages were defined using a base message type. No behavior there, little chance to get confused.


If OO is so 1990s why did JavaScript just add class support in ES5? Pretty much every big language is object oriented.

Classic DEV Post from Jan 29 '19

CSS With Feature Detection For Cross Browser Compatibility

Explore what cross-browser compatibility issues are and why do they occur, how cross browser CSS with feature detection is more favourable to browser detection.

Bertil Muth profile image
Agile coach and developer