AlgoViz

Multiple parents in single inheritance

medium 45 min

Java, C#, Kotlin, Swift and TypeScript allow one parent class.

Watch lesson4:35

Where it shows up

The same pattern appears across everyday software.

  • Java ArrayList

    Extends one class, AbstractList, and implements List, RandomAccess, Cloneable and Serializable. RandomAccess is a marker that tells algorithms that access by index is fast.

  • Java Collection.stream()

    A default method. Collections written before Java 8 got streams without a change.

  • Java HashSet

    Extends AbstractSet for the type, and is backed by a HashMap instance for the work: a parent and a part in one class.

  • Kotlin delegation

    `class Derived(b: Base) : Base by b` generates the forwarding methods. The manual warns that the delegate does not call your overrides.

  • C# default interface members

    Since C# 8 an interface can add a member with a body. Microsoft's tutorial gives safe growth of published interfaces as the main reason.

  • Go embedding

    Go has no subclasses. `bufio.ReadWriter` embeds a Reader and a Writer and gets the methods of both. The receiver stays the inner value.

  • Swift protocol extensions

    A class has one superclass and adopts many protocols. Extensions give protocols working methods for all types that conform.

  • Guava forwarding classes

    ForwardingList and its relatives ask for one method, `delegate()`, and forward the rest. Made for decorators.

  • PHP traits

    The manual calls traits a mechanism for code reuse in single inheritance languages. Name conflicts are fatal errors until `insteadof` chooses one.