Polymorphism
medium 45 minOne call, many behaviours: the caller asks for the same thing, and the object that receives the call decides how it is done.
Watch lesson4:47
Where it shows up
The same pattern appears across everyday software.
Java Collections
`List` is an interface. Code that takes a `List` works with `ArrayList`, `LinkedList` and the unmodifiable lists of `List.of`. The last ones show a weak contract too: `add` throws UnsupportedOperationException.
Linux Virtual File System
The kernel documentation describes the VFS as the layer that lets different file system implementations coexist. `open` and `read` are the same calls for all of them.
Python io
`io.IOBase` is the abstract base of all stream classes. A function that calls `read()` accepts a file, a `BytesIO` or any object with that method.
Python functools.singledispatch
Turns a function into a generic one and picks the version by the type of the first argument. Added by PEP 443.
Python operators
Methods such as `__add__` return NotImplemented for types they do not know, and Python then asks the other operand: double dispatch in the language.
Java threads
`Thread` accepts any `Runnable`. The library calls `run()` and has never heard of your class.
Java 21 switch
JEP 441: patterns in switch, with an exhaustiveness check for sealed types. The tool for closed sets.
Go interfaces
A type satisfies an interface by having its methods, without a declaration. The Go FAQ explains that there is no type hierarchy.