AlgoViz

Diamond problem

hard 50 min

Two parents share one ancestor, and a child has both parents.

Watch lesson5:10

Where it shows up

The same pattern appears across everyday software.

  • C++ iostream

    istream and ostream derive from ios virtually, and iostream derives from both: one shared ios, as cppreference explains. The best known diamond in production.

  • Python

    The tutorial says that all cases of multiple inheritance show a diamond, because every class inherits from object. The dynamic order makes sure that each parent is called only once.

  • Java LinkedList

    Implements List and Deque. JEP 431 gave both a `reversed()`, and LinkedList got its own version with its own return type.

  • C# interfaces

    The specification of default interface methods demands a most specific implementation, and names diamond inheritance as the conflict that the programmer must resolve.

  • Kotlin

    A class that inherits two implementations must override, and chooses with `super<Polygon>.draw()`.

  • Scala traits

    Superclass and mixins may share a supertype. The specification defines a class linearization and evaluates traits along it.

  • Perl mro

    The core module `mro` offers C3 as a method resolution order, next to the older depth first search.

  • Eiffel repeated inheritance

    A feature that arrives by two paths under one name is shared. Under two names it is replicated. The programmer chooses by naming.

  • Visual C++

    Microsoft's page on multiple base classes explains virtual bases with a lunch line: without them, a shared base is held twice and wastes space.

  • Ruby modules

    Including a module that is already among the ancestors does not add it again, so a shared module stands in the chain once.