Skip to content

Why inheritance never made any sense

There are three different types of inheritance going on.

  1. Ontological inheritance is about specialisation: this thing is a specific variety of that thing (a football is a sphere and it has this radius)
  2. Abstract data type inheritance is about substitution: this thing behaves in all the ways that thing does and has this behaviour (this is the Liskov substitution principle)
  3. Implementation inheritance is about code sharing: this thing takes some of the properties of that thing and overrides or augments them in this way. The inheritance in my post On Inheritance is this type and only this type of inheritance.

These are three different, and frequently irreconcilable, relationships. Requiring any, or even all, of them, presents no difficulty. However, requiring one mechanism support any two or more of them is asking for trouble.

A common counterexample to OO inheritance is the relationship between a square and a rectangle. Geometrically, a square is a specialisation of a rectangle: every square is a rectangle, not every rectangle is a square. For all s in Squares, s is a Rectangle and width of s is equal to height of s. As a type, this relationship is reversed: you can use a rectangle everywhere you can use a square (by having a rectangle with the same width and height), but you cannot use a square everywhere you can use a rectangle (for example, you can’t give it a different width and height).

Notice that this is incompatibility between the inheritance directions of the geometric properties and the abstract data type properties of squares and rectangles; two dimensions which are completely unrelated to each other and indeed to any form of software implementation. We have so far said nothing about implementation inheritance, so haven’t even considered writing software.

Smalltalk and many later languages use single inheritance for implementation inheritance, because multiple inheritance is incompatible with the goal of implementation inheritance due to the diamond problem (traits provide a reliable way for the incompatibility to manifest, and leave resolution as an exercise to the reader). On the other hand, single inheritance is incompatible with ontological inheritance, as a square is both a rectangle and an equilateral polygon.

The Smalltalk blue book describes inheritance solely in terms of implementation inheritance:

A subclass specifies that its instances will be the same as instances of another class, called its superclass, except for the differences that are explicitly stated.

Notice what is missing: no mention that a subclass instance must be able to replace a superclass instance everywhere in a program; no mention that a subclass instance must satisfy all conceptual tests for an instance of its superclass.

Inheritance was never a problem: trying to use the same tree for three different concepts was the problem.

“Favour composition over inheritance” is basically giving up on implementation inheritance. We can’t work out how to make it work, so we’ll avoid it: get implementation sharing by delegation instead of by subclassing.

Eiffel, and particular disciplined approaches to using languages like Java, tighten up the “inheritance is subtyping” relationship by relaxing the “inheritance is re-use” relationship (if the same method appears twice in unrelated parts of the tree, you have to live with it, in order to retain the property that every subclass is a subtype of its parent). This is fine, as long as you don’t try to also model the problem domain using the inheritance tree, but much of the OO literature recommends that you do by talking about domain-driven design.

Traits approaches tighten up the “inheritance is specialisation” relationship by relaxing the “inheritance is re-use” relationship (if two super categories both provide the same property of an instance of a category, neither is provided and you have to write it yourself). This is fine, as long as you don’t try to also treat subclasses as covariant subtypes of their superclasses, but much of the OO literature recommends that you do by talking about Liskov Substitution Principle and how a type in a method signature means that type or any subclass.

What the literature should do, I believe, is say “here are the three types of inheritance, focus on any one of them at a time”. I also believe that the languages should support that (obviously Smalltalk, Ruby and friends do support that by not having any type constraints).

  • If I’m using inheritance as a code sharing tool, it should not be assumed that my subclasses are also subtypes.
  • If I am using subtypes to tighten up interface contracts, I should be not only allowed to mark a class anywhere in the tree as a subtype of another class anywhere in the tree, but required to do so: once again, it should not be assumed that my subclasses are also subtypes.
  • If I need to indicate conceptual specialisation via classes, this should also not be assumed to follow the inheritance tree. I should be not only allowed to mark a class anywhere in the tree as a subset of another class, but required to do so: once again, it should not be assumed that my subclasses are also specialisations.

Your domain model is not your object model. Your domain model is not your abstract data type model. Your object model is not your abstract data type model.

Now inheritance is easy again.

{ 15 } Comments

  1. Bar | March 16, 2018 at 9:48 pm | Permalink

    Can you perhaps provide examples for the last 3 points in the end?
    It will help a lot to understand your meaning.

    Thanks!

  2. Vanja | March 17, 2018 at 9:10 am | Permalink

    Examples would be very much appreciated 🙂

  3. scooter | March 17, 2018 at 12:02 pm | Permalink

    The most lucid, succinct and accessible description of this nest of misunderstanding I’ve yet seen. Thanks for posting.

  4. Foo | March 17, 2018 at 1:22 pm | Permalink

    Inheritance and composition are both about finding common components from the data or from the functionality. I found it delightful to find out the link between the category theory, class inheritance and composition. Here I have the connection written down: https://github.com/kummahiih/python-domain-equations and here is the definition of a category if you are a lazy googler: https://en.wikipedia.org/wiki/Category_(mathematics)#Definition

    One difference between inheritance and composition you don’t mention here is the life cycle of the functionality or the data: if the functionality might have different life cycle, bring it in with dependency injection, if your data has a different life cycle get it in as a parameter and use composition.
    The usual reason of the different life cycle is the support for the unit testing.

  5. Franklin Chen | March 17, 2018 at 8:02 pm | Permalink

    In 1996, the programming language Objective Caml (the name later shortened to OCaml ) was invented to try to separate notions of subtyping, inheritance, etc. Could you add your thoughts on what you think of its innovations, and why they didn’t become popular?

  6. Christopher Froehlic | March 18, 2018 at 7:21 pm | Permalink

    (Your name field doesn’t accommodate my name: Christopher Froehlich–no big deal)

    I think your post is interesting at a philosophical level, and I would even to most extents agree with you. I have a hard time cogitating big ideas without having concrete examples to back them, which is my problem–not yours.

    There is one place where I absolutely love inheritance: at the database tier. I love it in one very specific way, which is that I can define all my entities as inheriting from a single base class which defines nothing except a unique Id. It’s a very simple sort of behavioral expectation: do you exist? It allows for some rather elegant simplifications of logic at higher layers when it comes to crafting resources and interfaces.

    I can’t really speak to the rest of the argument as I have no other concrete examples with which to speak from, but in this one specific case–inheritance serves a purpose and serves it well (imo).

  7. Valentin | March 19, 2018 at 12:43 pm | Permalink

    Hey, you say that Ruby supports those three types of inheritance, how are they implemented?
    I suppose that 1) and 3) are implemented via traditional inheritance and we can achieve 2) by using modules.
    Am i right ?
    Thank you.

  8. Tom Whitmore | March 20, 2018 at 3:56 am | Permalink

    Great conceptual illustration of the problems — it also shows how poor traditional OO examples of “Cat and Dog extend Animal” and “Square extends Rectangle” are.

    However, I find one of the key problems with inheritance is trying to apply it to entire entities — it breaks down for non-trivial entities having multiple interactions, since the API & code reuse needs of each interaction may be different.

    The solution I bring to bear is to factor interactions separately, designing their interfaces on a behavioral basis. This seems highly effective in allowing robust substitution, code reuse where warranted, and independent variation of different entity interactions.

    An article & commentary on this approach:
    http://literatejava.com/oo-design/oo-design-is-more-about-doing-than-being/
    http://literatejava.com/java-language/why-inheritance-never-made-any-sense/

  9. Michael Klunk | March 20, 2018 at 1:14 pm | Permalink

    Do a study on Design Patterns. The head start series has a VERY good one for the layman using Java, but its dated. You can see the examples in a real word context where inheritance is a failure or creates many complexities as projects get larger that can be resolved through interfaces and abstractions. Many of the principles cited here are taught in a DP course or in the book.

  10. Víctor Adrián | March 31, 2018 at 6:25 am | Permalink

    I think this is backwards:

    “You can use a rectangle everywhere you can use a square, but you cannot use a square everywhere you can use a rectangle.”

    It should read like:

    “You cannot use a rectangle everywhere you can use a square (since the square is a special case of a rectangle [need all 4 sides to be of same length]), but you can use a square everywhere you can use a rectangle (since rectangles don’t have restrictions about side length, every square IS a rectangle anyway).”

  11. Jeff | April 4, 2018 at 6:13 am | Permalink

    Victor, you can’t use a Square anywhere you would use a Rectangle. Consider:

    Myclass::SizeOfAffectedArea( Rectangle &r );

    If you pass a square in as the argument to receive the affected area, either its height or its width will be wrong, since the method will attempt to update them separately.

    Oh, and Graham, a football is an ellipsoid.

  12. Jeff | April 4, 2018 at 6:28 am | Permalink

    After hitting send, of course, I realized that I should not have said:

    "you can’t use a Square anywhere you would use a Rectangle"

    because you “can”, but

    "you shouldn’t use a Square anywhere you can use a Rectangle"

    because it (probably) won’t give you the result you want.

  13. Greg Lomow | April 5, 2018 at 4:08 am | Permalink

    This is one area where C++ had the right idea since it provided for Public inheritance (substitution) and Private inheritance (reuse), which you could make explicit in your code (although 90+% of programmers never realized the importance of and the differences between these two language constructs).

  14. Graham | April 9, 2018 at 12:34 pm | Permalink

    This is a defence of my thesis :). You can use the concept square everywhere you can use the concept rectangle, because a square is a rectangle with additional constraints. You cannot use the type square everywhere you can use the type rectangle, because a square has stricter invariants than a rectangle. The fact that this is confusing is an argument against trying to use inheritance to model both of these relationships.

  15. Graham | April 9, 2018 at 12:34 pm | Permalink

    That’s a handegg.

Post a Comment

Your email is never published nor shared. Required fields are marked * Comments are moderated; please make sure that your post is civil and valuable before submitting it to improve the chance it will be accepted.