AlgoViz

Multiple inheritance

hard 50 min

A class with several parent classes gets the types, the methods and the fields of all of them.

Watch lesson4:37

Where it shows up

The same pattern appears across everyday software.

  • Python socketserver

    `class ThreadingUDPServer(ThreadingMixIn, UDPServer)`. The documentation explains that the mix-in comes first because it overrides a method of the server.

  • Python http.server

    ThreadingHTTPServer is HTTPServer with threads, made with the same mix-in. The second use is what makes a mix-in worth having.

  • Python collections.abc

    Write a few abstract methods and get the mix-in methods free: a Mapping with three methods gets `keys`, `items`, `get` and more.

  • Django class based views

    Views are built from mixins, and the documentation warns that overlapping methods and the MRO must be considered before combining them.

  • scikit-learn

    An estimator is a mixin plus BaseEstimator. The developer guide says that mixins should be on the left and BaseEstimator on the right, for the MRO.

  • Flask-Login

    UserMixin gives a user class the default properties and methods that the library asks for.

  • Python Enum

    IntEnum members are also integers: a value type and Enum are mixed in one class.

  • Qt

    The moc tool requires QObject to be the first base, and only one base may be a QObject. A framework that allows two parents, with rules.

  • Eiffel

    The tutorial's ARRAYED_LIST inherits from LIST and ARRAY and renames `count` to `capacity` at the point of inheritance, so the clash is gone.

  • C++ Core Guidelines

    C.135 recommends several bases for distinct interfaces, C.136 for the union of implementation attributes.