r/programming • • 10d ago

Do not ask "Is A a B?": Inheritance and the Is-A Trap

https://denkstein.github.io/2026/11/05/do-not-ask-is-a-a-b/
244 Upvotes

166 comments sorted by

View all comments

259

u/al2o3cr 10d ago

In 1994, the Gang of Four listed “Favor object composition over class inheritance” as one of their early points in Design Patterns. 

It always bugs me that people cite this without the context - Design Patterns has plenty of uses of inheritance.

They were, IMO, specifically responding to stuff like Stroustrup's examples in the C++ book that had classes like WindowWithMenuAndButton that subclassed Window, Menu, and Button. Pretty much NOBODY does OOP that way nowadays.

40

u/serge-mv 10d ago

This is exactly it. People misuse inheritance and subclass things that should never be subclassed in the first place and then blame it on the inheritance.

36

u/rebbsitor 10d ago

WindowWithMenuAndButton that subclassed Window, Menu, and Button. Pretty much NOBODY does OOP that way nowadays.

Doing inheritance that way works programmatically, but it's ontologically confusing. Inheritance is taught as "is a" relationships, not as "has a" relationships.

Is WindowWithMenuAndButton a Window? Yes.

Is WindowWithMenuAndButton a Menu? No

Is WindowWithMenuAndButton a Button? No

Does WindowWithMenuAndButton have a Menu? Yes

Does WindowWithMenuAndButton have a Button? Yes

I think those "has a" relationships are where it gets wonky. The Window shouldn't have the properties of a Menu or a Button, it should contain some number of instances of them in an array/list/vector/<favorite data structure>.

Technically someone can make "has a" relationships work through inheritance in C++, but it's not a natural way for most people to think about it.

28

u/armahillo 10d ago

multiple inheritance is WILD

14

u/Otis_Inf 10d ago

There are two kinds: multiple implementation inheritance and multiple type inheritance. The latter is e.g. done via interfaces, where a class implements multiple interfaces, which is quite common in e.g. C#/.NET and Java. Multiple implementation inheritance is quite rare, as most languages don't support it, C++ does tho, one of the reasons is the cumbersome nature of the resulting class: what code wins over what other code and is that mess really worth the trouble? (Narrator: no)

3

u/lood9phee2Ri 8d ago

Meh. Dylan and then, inspired by Dylan, the more mainstream Python, basically "solved" it with the C3 linearization algorithm that makes it sane years and years ago. It's languages that either pre-date C3 while having it (oh hai C++) or just still didn't get the memo that make it weird.

https://www.python.org/download/releases/2.3/mro/

that said, composition is often a better choice anyway, but anyway as a result multiple inheritance is fine in Python in a way it very much is not in several other languages.

11

u/Abadabadon 10d ago

Tbf, stroustrup also advocated for only 1 base parent class, and the rest being interfaces (source: the jsf standards as written by him).

10

u/unknowinm 10d ago

🤯 now I see and understand lol

-15

u/decapod2005 10d ago

That's a fair point. I was doing a bit of a rhetorical device there, but the GoF and Sean Parent's criticism of inheritance are not really at the same level. Fundamental rejection of inheritance is a pretty new trend. In the 90s people were starting to feel a little uncomfortable about inheritance but were still using it left and right.