Multiple inheritance is an intermediate-to-advanced topic and therefore, the material in this chapter will NOT be on an exam. Also, the material in this chapter will NOT be required in solutions to programming assignments.
Multiple inheritance is the ability of a class to inherit from multiple, independent classes. It is NOT the same concept as chained or vertical inheritance as shown in the Classes & Objects chapter with Parent<-Child<-GrandChild classes. An example of multiple inheritance is a SoftwareDeveloper class that inherits from two classes: Employee class and Alumni class. The Employee class and Alumni class are independent, unrelated classes.
Multiple inheritance in Python is implemented via an approach known as C3 Linearization. It specifies the order of method calls based on graph relationships. In Python, this is know as Method Resolution Order (MRO). In the second code example below, MRO is demonstrated.
In the first code example, a simple version of multiple inheritance is provided. The class named SubClass inherits from both SuperClass1 and SuperClass2. Method calls to methods in both super classes are represented.
Here's the output.
In the second code example, a more involved version of multiple inheritance is shown. Code the example and follow the program flow using the debugger. Notice how the inheritance progression as specified by MRO is not always the order anticipated. For instance, both parent constructors are called by lines 44 and 47 but only the constructor of SuperClass2 is called by line 50. Developers should consider the complexity of C3 Linearization before implementing multiple inheritance in production systems.
By the way, the symboladjacent to a class declaration means that the class is being subclassed (is being inherited). The SuperClass1and SuperClass2 classes below are both being inherited by SubClass. The symboladjacent to methods means the method is being overridden by a method of the same name in a subclass. Method overriding can be observed in the __init__ methods.
Here's the output.