Get startedGet started for free

Multiple Inheritance

1. Multiple Inheritance

Earlier, we practiced inheriting the functionality of a single class. But what if we'd like to inherit more than one class?

2. Multiple inheritance

When a class inherits from more than one parent, it is known as multiple inheritance. Multiple inheritance allows for a class to directly inherit the methods and attributes of more than one parent class, helping reduce code sprawl and provide consistent functionality. Later, we'll define an Intern class that inherits from both Employee and Student. Intern will inherit the functionality of each parent and can still implement additional attributes or methods, as needed. With multiple inheritance, an "is-a" relationship between parents and children should still be maintained. Here, Intern is an Employee, as well as a Student.

3. Multiple inheritance

To inherit both the Employee and Student classes, the Intern class is defined with two parent class names in parentheses. Then, both the Employee and the Student constructors are called, allowing the functionality of each to be used in the Intern class. If needed, additional attributes or methods (such as the duration attribute) could be defined in the Intern class.

4. Creating an Intern object

Once an Intern object is created, both the begin_job method from the Employee class and the add_course method from Student can be executed.

5. Multilevel inheritance

Multilevel inheritance is another form of multiple inheritance. A class implements multilevel inheritance when it inherits from a class which itself inherits from a parent class, thus becoming a grandchild. Later, we'll define a Manager class that inherits Employee, which previously inherited Person. Multilevel inheritance should also maintain an "is-a" relationship. Here, Manager is an Employee, which itself is a Person. Manager will receive all functionality from both classes, but can still implement additional attributes and methods.

6. Multilevel inheritance

Here, Manager inherits Employee, which is a child of Person. The Employee constructor is called, which invokes the Person constructor. Since Employee maintains the functionality of Person, Manager inherits the functionality of both classes, including the introduce and change_position methods. Just like with multiple inheritance, Manager can implement it's own attributes and methods, such as number_reports.

7. Method resolution order (MRO)

What if parent or child classes implement a method with the same name? Which method will be used? The order in which Python determines which method to use in this scenario is known as method resolution order, or MRO. Python uses the C3 algorithm to determine a classes MRO, but all you need to remember are two basic rules; children will be searched first, and parent classes will be searched left-to-right as they were defined in the class statement. To find a class's MRO, we can use the mro method, or the __mro__ attribute.

8. Method resolution order (MRO)

To find the MRO for the Intern class, we'll use the mro method. The result is a list of class names, in the order which Python will search when executing a certain method. Since Intern is a child class, it's searched first. Then, Python searches the Employee class, as it's the left-most parent class in the Intern definition, followed by the Student class. Using the __mro__ attribute, we can do something similar. This time, the return value will be a Tuple, but will maintain the same order the mro method returns.

9. Let's practice!

Let's practice implementing multiple and multilevel inheritance with a few examples!