Composition over Inheritance

The cluster debates the use of inheritance in object-oriented programming, with most comments advocating for composition, interfaces, traits, or other alternatives over inheritance due to issues like tight coupling and fragility.

➡️ Stable 0.7x Programming Languages
4,255
Comments
20
Years Active
5
Top Authors
#2622
Topic ID

Activity Over Time

2007
7
2008
18
2009
58
2010
93
2011
129
2012
142
2013
142
2014
296
2015
175
2016
249
2017
242
2018
268
2019
290
2020
335
2021
372
2022
260
2023
391
2024
337
2025
426
2026
25

Keywords

OO ApplicationController e.g wikipedia.org UI OOP CompositionIn JZ91 CompositionInsteadOfInheritance inheritance composition oop oo reuse implementations polymorphism code patterns interfaces

Sample Comments

jbooth Feb 15, 2016 View on HN

Alternate view:https://en.wikipedia.org/wiki/Composition_over_inheritance

Inheritance is not necessarily the wrong abstraction, too.

_y5hn Apr 30, 2021 View on HN

Inheritance is just a tool to mostly avoid. It tightly couples different class implementations. So will make later refactors error-prone and tend to scatter logic. It's misunderstanding OO as messaging between objects.Using composition or lay out data differently may yield designs with more desirable properties. There's no one answer and it depends.

nine_k Dec 28, 2023 View on HN

Inheritance is likely the worst idea of OOP's implementations.The right way to achieve the same and better results is interfaces / typeclasses / traits. They work pretty well in languages from Rust and Haskell to Typescript, Kotlin, Java, and even Go. (Go is a bit special because you don't even have to declare that your class implements an interface, as long as it actually has a set of methods with right signatures.)Ideally, all public methods should only describe param

mguterl Jul 20, 2012 View on HN

Inheritance is not always the right solution.

halayli Mar 19, 2016 View on HN

Inheritance isn't terrible if you design your classes properly.

gnaritas Oct 12, 2010 View on HN

No, there's a time and place for inheritance, just not as often as composition.

groone Jan 16, 2023 View on HN

Inheritance is a concept of OOP not programming itself. And yes I much prefer composition.

baggy_trough Sep 2, 2020 View on HN

Inheritance is usually bad, but encapsulation and composition are good.

gnaritas Oct 12, 2017 View on HN

Inheritance is not essential to OO; it is merely a method of sharing code.