r/ProgrammingLanguages • u/Inconstant_Moo 🧿 Pipefish • Mar 07 '26
Discussion Inheritance and interfaces: why a giraffe is not a fish
In biology (no, I'm not lost, bear with me) the obvious way to form groups of species is to group together things which are more closely related than anything outside the group. This is called a clade. For example, "things that fly" is not a clade, birds and bats and butterflies are not each other's nearest relatives. However, for example, "camels and lamines" is a clade: camels (Bactrian and dromedary) are the closest living relatives of the lamines (llamas, alpacas, etc) --- and, of course, vice-versa.
The reason it's possible to arrange species like this is that since evolution works by descent with modification, it automatically has what we would call single inheritance. In OOP terms, the classes BactrianCamel and Dromedary and Llama and so forth have been derived from the abstract Class Camelids.
(I will use Class with a capital letter for the OOP term to avoid confusing it with the biological term.)
This grouping into clades by inheritance is therefore the obvious, natural, rational, and scientific way of classifying the world. And it follows immediately, of course, that a giraffe is a fish.
* record scratch *
Well, it's true! If we look at that branch of the tree of life, it looks like this:
──┬───── ray-finned fish
└──┬── lobe-finned fish
└── amphibians, mammals, reptiles, birdsIt follows that if Fish is an abstract Class at all, then Giraffe and Human and Hummingbird must all be derived from it, and are all Fish together.
This illustrates a couple of problems with the vexed question of inheritance. One is that single inheritance only works in biology at all because biology naturally obeys single inheritance: the "tree of life" is in fact a tree. The other is that it often makes no sense at all for practical purposes. Every time in human history anyone has ever said "Bring me a fish!", their requirements wouldn't have been satisfied by a giraffe. They have an intuitive idea in mind which scientists might call a grade and which we might call an interface (or trait or typeclass depending on what exactly it does, what language you're using, and who taught you computer science).
The exact requirements of a Fish interface might depend on who the user: it would mean one thing to a marine biologist, another to a fisherman (who would include e.g. cuttlefish, and anything else he can catch alive in his net and sell at the fish market) and to someone working in a movie props department it means anything that will look like a fish on camera. All of these interfaces cut across the taxonomy of inheritance.
Oh look, we're back to language design again!
By "interface" I mean something somewhat broader than is meant in OOP, in that the spec for an interface in OOP is usually about which single-dispatch methods you can call on an Object. But it doesn't have to be just that: in a functional language we can talk about the functions you can call on a value, and we can talk about being able to index a struct by a given field to get a given type (I think that's called row polymorphism?) and so on. In short, an interface is anything that defines an abstract Class by what we can do with the things in it.
And so when we define our various objects, we can also define the interface that we want them to satisfy, and then we (and third parties using the library we're writing) can use the interface as a type, and given something of that type we can happily call the methods or whatever that the interface satisfies, and perform runtime dispatch, and surely that solves the problem?
After all, that's how we solved the whole "what is a fish" question in the real world, isn't it? On the Fifth Day of Creation, when we created fish, we also declared what features a thing should have in order to be considered a fish and then when we created each particular species of fish, we also declared that it satisfied that definition, thus definitively making it a fish. Problem solved!
* record scratch again *
No, we didn't. That was satire. But it's also how interfaces usually work. It's how e.g. I was first introduced to them in Pascal. It's how they work in Java. You have to define the interface in the place where the type is created, not in the place where it's consumed. The question of whether something is a fish must be resolved by its creator and not by a seafood chef, because if you don't make the species, you can't decide if it's a fish or not.
Or, in PL terms, if you don't own the type, then the creator of the third party library is playing God and he alone can decide what Interfaces his creations satisfy. Because typing is nominal, someone needs to say: "Let there be Herrings, and let Herrings be Fish."
The alternative is of course to do it the other way round, and define what it takes to satisfy the interface at the place where the interface is consumed. This idea is called ad hoc interfaces. They are structural. These are to be found (for example) in Go, which takes a lot of justified flak from langdevs for its poor decisions but has some good ideas in there too, of which this is one. (To name some others, the concurrency model of course; and I think that syntactically doing downcasting by doing value.(type) and then allowing you to chain methods on it should be standard for OOP-like languages. But I digress.)
Ad-hoccery changes what you do with interfaces. Instead of using big unwieldy interfaces with lots of qualifying methods to replace big unwieldy base classes with lots of virtual methods, now you can write any number of small interfaces for a particular purpose. You can e.g. write an interface in Go like this:
type quxer interface {
qux() int
}... for the sole purpose of appearing in the signature of a function zort(x quxer).
And now suppose that you depend on some big stateful third-party object with lots of methods. You want to mock it for testing. Then you can create an interface naming all and only the methods you need to mock, you can create a mock type satisfying the interface, and the third-party object already satisfies the interface. Isn't that a lovely thought?
In my own language, Pipefish, I have gone a little further, 'cos Pipefish is more dynamic, so you can't always declare what type things are going to be. So for example we can define an interface:
newtype
Fooable = interface :
foo(x self) -> intThis will include any type T with a function foo from T to int, so long as foo is defined in the same module as T.
So then in the module that defines Fooable, we can write stuff like this:
def
fooify(L list) -> list :
L >> fooThe function will convert a list [x_1, x_2, ... x_n] into a list [foo(x_1), foo(x_2), ... foo(x_n)], and we will be able to call foo on any of the types in Fooable, and it'll work without you having to put namespace.foo(x) to get the right foo, it dispatches on the type of x. If you can't call foo on an element of L, it crashes at runtime, of course.
Let's call that a de facto interface, 'cos that's two more Latin words like ad hoc. (I have reluctantly abandoned the though of calling it a Romanes eunt domus interface.)
It may occur to you that we don't really need the interface and could just allow you to use foo to dispatch on types like that anyway. Get rid of the interface and this still works:
def
fooify(L list) -> list :
L >> fooThis would be a form of duck typing, of course, and the problem with this would be (a) the namespace of every module would be cluttered up with all the qualifying functions; (b) reading your code now becomes much harder because there's now nothing at all in the module consuming foo to tell you what foo is and where we've got it (c) there's no guarantee that the foo you're calling does what you think it does, e.g. in this case returning an integer. We've done too much magic.
De facto interfaces are therefore a reasonable compromise. Pipefish comes with some of them built-in:
Addable = interface :
(x self) + (y self) -> self
Lenable = interface :
len(x self) -> int
// etc, etc.And so instead of single inheritance, we have multiple interfaces: if I define a Vec type like this:
newtype
Vec{i int} = clone list :
len(that) == i
(v Vec{i}) + (w Vec{i}) :
Vec{i} from L = [] for j::x := range v :
L & (x + w[j])... then Vec{2}, Vec{3} etc satisfy Addable and Lenable and Indexable and so on without us having to say anything. Whereas in an object hierarchy, the important question would be what it's descended from. But why should that matter? A giraffe is not a fish.
---
If I don't link to Casey Muratori's talk The Big OOPs: Anatomy of a Thirty-five-year Mistake, someone else will. He dives into the roots of the OOP idea, and the taxonomy of inheritance in particular.
And it all seems very reasonable if you look at the original use-case. It works absolutely fine to say that a Car and a Bus and a Truck are derived from an abstract Class Vehicle. Our hierarchy seems natural. It almost is. And Cat and Dog are both kinds of Animal, and suddenly it seems like we've got a whole way of dividing the real world up that's also a static typesystem!
Great! Now, think carefully: is the Class Silicon derived from the abstract Class Group14, or from the abstract Class Semiconductors? 'Cos it can't be both.
9
u/mirpa Mar 07 '26
Are you familiar with ad-hoc polymorphism like type classes in Haskell?
Num class defines (+)
length for Foldable class
31
u/nerdycatgamer Mar 07 '26
Programming is not a classification of a naturally arising structure; it is a system that we design for specific purposes. Inheritance (whether you like it or not) is not supposed to be a perfect description of the universe, but a method for code reuse and polymorphism.
That's not the original use case of inheritance; that is a contrived example that is used by professors who don't actually write code to explain inheritance to first year students who don't actually write code which they then parrot in interviews. If you actually look back, one of the earliest examples of inheritance was for generic data structures without generics. They would make a data type inherit from LinkedListNode to create a linked list of that data type, and we may hear that and shutter at the thought, but it is another example of something that inheritance can do and further exemplifies my point that inheritance is not some perfect description of the universe (because your particular computer program is not an abstract description of the entire universe)
Whether Circle inherits from Elipse or vise versa is not some impossible question that shows why inheritance works; the answer varies depending on the program and what it is meant to do. Depending on your purposes it may make more sense for a Circle to be a special type of Elipse or an Elipse to be a special type of Circle. Whether
6
u/Tasty_Replacement_29 Bau Mar 08 '26
I agree that interfaces (traits) are much more useful than inheritance. (Even thought I'm a long-time Java user). One open question is: nominal or structural typing, or something in between? (Nominal: interfaces are explicitly listed in the type, as in Java; structural: not explicitly listed, as in Go). Another question is: what about generics / templates.
Structural: The main advantage (if we exclude generics / templates) is that libraries can work with types they didn’t define: you can add an interface to a type in a foreign module. To solve this, I think it's best to put some restrictions, like: allow adding interfaces to foreign types, but only locally in the module, and only explicitly. Together with generics (templates), I think most use cases are resolved nicely.
Nominal typing is more explicit, but safer, for large codebases; structural typing is great for ad-hoc things, but also more dangerous. Not dangerous for memory safety or runtime exceptions, but dangerous in that you can make a mess: you can accidentally implement an interface you did't mean to, and then accidentally call it. The typical example is with "Closeable" and "Close", and then you by accident have "Window" to be a closeable just because it happens to have a close method. Another example is when refactoring, you can by accident refactor things you didn't mean to, or forget to refactor things you wanted.
(Btw do you know "I apologize for such a long letter - I didn't have time to write a short one.")
14
u/KittenPowerLord Mar 07 '26
Why are the comments so awful? I liked the article, it's written very well, the narrative bit is quite cool!
Tbh I don't entirely get the "Get rid of the interface and this still works" thing, the code snippet is the same. Was the first snippet supposed to specify that we are using `Fooable.foo`?
4
u/syklemil considered harmful Mar 08 '26
Comments here notwithstanding, isn't this either breaching open doors or tilting at windmills?
There aren't that many popular actually new languages, but both Go and Rust seem to have, with not all that much real argument, omitted inheritance. Other newer languages that get some mentions here and there, like Zig and Gleam, also don't seem to feature it.
So inheritance was kind of big in the 80s-90s, and we still use a lot of … millennial languages, but going forward it seems less likely to show up in a new language that actually gets popular.
I think the problem for some people, like Muratori, is that they're using a language that was very much built around the idea of inheritance. It is possible, with great effort, like Google shows, to omit certain language features from one codebase. But it ultimately winds up being a large dialect; in the language ecosystem at large, you're still going to get people using any combination of language features.
C++ in particular is never going to tear anything out: The committee is dominated by the stewards of legacy, and the orgs that did want to change things up, like Google, have left.
So the two big audiences seem to be
There might be some people out there cooking up a new language based around that kind of inheritance, but I get the impression they're so rare that personal interactions would be better than broadcasts.
7
u/Clementsparrow Mar 07 '26
haddock interfaces are the best!
17
u/todo_code Mar 07 '26
Dear Lord that was too much to read. Evolution is not single inheritance so starting off on the wrong foot
28
u/SymbolicDom Mar 07 '26
In oop programming don't model classes and objects after real world objects. Make them after the data and how it needs to be encapsulated. Only use inheritance when you need it for the code. If you have a game with tons of monsters running around, it may be better having to have one object for all monsters and arrays with the data for the monsters imstead of an object for each monster.
4
u/johnwcowan Mar 07 '26
Animals, yes; plants, not so much. See the citrus family trees at https://www.math.uni-bielefeld.de/~frettloe/papers/zitrustaxonomie.pdf.
15
u/glasket_ Mar 08 '26
Honestly, I think you probably should've dropped the evolution stuff because it's mostly a distraction from what you're actually talking about. It also has some problems, like grades/paraphyletic groups aren't really equivalent to interfaces; they exclude certain descendants. Fish is basically the osteichthyes clade minus tetrapods, so the "natural representation" would actually be tetrapods and fish both inheriting from vertebrates, rather than fish being an interface.
Interfaces are more like polyphyletic groups, with independently developed common traits. They can pop up at any branch in the tree rather than being anchored to a particular ancestor.
4
u/_stichomythia Mar 08 '26
This guy's been attacking the same straw man for over a decade now, sheesh
5
u/Ok-Jacket7299 Mar 07 '26
8
u/zyxzevn UnSeen Mar 07 '26
Forget about the biology and physics of things, those are bad examples. Pure OOP is about interfaces.
Based on Smalltalk's early success, we can look at why it worked.
Inheritance works better if you only share the common interfaces. So things with the same function-set can be used in the same way. For example: File.Open() File.Close() and Stream.Open(), Stream.Close(). In Go the common functions of different types are directly mapped to an abstract class.
Smalltalk also had ducktyping, so you could mix any type with another type. "If it talks like a duck, it is a duck".
The main idea was that you can develop fast and did not need to care much about execution speed. Though, you did need to care about memory size in early computers. If you needed speed, you could just use C at the time. Later came JIT compilers.
With modern types this would be implemented with abstract classes and used in "composition instead of inheritance". Additionally Scala and such have "types of types" to define lists of things and such. So look at Scala (or Kotlin) if you want to see how types mix with Pure OO and functional programming.
Additionally Smalltalk has closures, which allows functions to be passed around. It was based on Lisp and functional programming. This avoid a lot of useless classes, like the many "design patterns" for C++ and Java.
Even without OOP, you need to look at Domains if you want to define your entities well. So if you want to describe a Car and Bus and Truck in your system, then what do you want to store about them? Are they for sale? Do you need to track hired cars? Do you need to simulate road traffic? Those are very different Domains.
A lot of data design gets messy when Domains are mixed up. And programmers often like to make stuff without fully considering the Domain.
With OOP this Domain leakage happens more often, because it is easier to declare all kinds of entities and relationships between them. Just imagine a database of vehicles. Someone "smart" might just make different database tables for each type of Car, Bus and Truck. While in practice it can all be one table. It can also be just one class.
3
u/Isogash Mar 08 '26
Inheritance in programming is not supposed to be used to model real life types, it's there to make code reusable and behaviour dynamically switchable. The common examples are designed to intuitively explain what inheritance means, not give examples of how to model these concepts for real.
Programming types are static, for the types of structures in your code itself. If you are modelling some type hierarchy then that's a data problem, and your code should be an abstract model of that data structure. For example, the classes in some taxonomy software are more likely to look like components of a graph e.g. Node, Edge (or simply use an existing graph database.)
The problem you describe is somewhat real though. Some languages have added a feature that allows you to add your own interface/trait implementations to external classes/structs, so that their behaviour can be extended by your code.
For example Rust allows stuff like
impl Cook for Fish { ... }(not that you should be modelling cooking and fishing in your code statically)2
u/ReedTieGuy Mar 12 '26
I don't understand why there are so many hate comments, this is a great writeup.
1
u/tobega Mar 08 '26
LOL, good description and fun read, if somewhat lengthy!
1
u/middayc Ryelang Mar 08 '26
I liked reading this because I don't have formed answers on the subject. I mostly avoid typical OO, but sometimes it's applicable.
1
u/Kapendev Mar 08 '26
That's an interesting nerd topic.
1
u/esotologist Mar 09 '26
Lovely explanation! I'm saving this for later
0
u/fnordstar Mar 08 '26
Didn't read it all but doesn't rust solve this problem with traits? A crate defining types can implement any third party trait for their types. Another crate can implement any of it's own traits for those foreign types.
0
u/[deleted] Mar 08 '26
[deleted] — view deleted comment