In object-oriented programming, we have learned the basics of classes and objects and how classes are interlinked with each other in the form of inheritance. In this article, we will learn about multiple inheritances in python and how to use this concept in your program.

Multiple Inheritance

Similar to C++, a class can be derived from more than one base class which is known as multiple inheritances.

As we have already studied that inheritance inherits all the features of the base class into the derived class. The syntax of multiple inheritances is similar to single inheritance. Let’s take an example of multiple inheritances.

Example

class Car1:
    pass

class Car2:
    pass

class Garage(Car1, Car2):
    Pass

In the above example

1. We declare two classes Car1 and Car2

2. We inherit the Car1 and Car2 classes from the Garage class

3. All the properties of Car1 and Car2 are now passed to Garage class.

Multiple Inheritance vs Multilevel Inheritance

These are very important as well as confusing questions often asked by the interviewers etc. So, python allows inheriting a class from another class that is also derived from another class. This is called multilevel inheritance.

In multilevel inheritance, the features of the main branch are inherited into the newly derived class. Let’s take an example of this concept.

Example

class Car:
    pass

class Car1(Base):
    pass

class Garage(Derived1):
    pass
    

In the above example, we derive the Derived1 class from the base class.

Method resolution method in python

It is a very interesting fact to learn that every python class is derived from a base class. You might be thinking, what base class it is? Well, this base class is called object class and is the most base type.

Example

print(issubclass(list,object))

Output: True

print(isinstance(1.2,object))

Output: True

print(isinstance("Python",object))
Output: True

Searching in multiple inheritance

While the execution of the python program containing multiple inheritance, if the program needs to look for any attribute, it follows depth-first mechanism. That is, its searches in the class itself and if not found, continue into the parent classes. This avoids searching the attribute in the same class again and again.

Linearization of Multiderived class

In the previous example, the search order of the class is Garage, Car1, Car2, and then the object class. This method is also known as the linearization of Garage class and the rules to identify this order of execution of the request is called Method Resolution Order (MRO).

MRO promotes monotonicity and avoids any preceding ordering. It makes sure that the class child class must come before its parent class. In the case of multiple parent classes, the order of tuples of base classes is set in a way that it appears before its parent.

MRO of a class can be represented as __mro__ attribute or simply mro() function, Below example returns a tuple. Lets take an example:

(<class '__main__.MultiDerived'>,
 <class '__main__.Base1'>,
 <class '__main__.Base2'>,
 <class 'object'>)
 
 [<class '__main__.MultiDerived'>,
 <class '__main__.Base1'>,
 <class '__main__.Base2'>,
 <class 'object'>]
 

Multiple inheritance get complex or tricky very quickly. A simple solution to this is allowed is to write a mixin. A mixin can be defined as a type of class that does not focus on the position of the hierarchy. You can provide more than convenient,

Example

class CalculateAreaMixin:
    def area(self):
        area = 0
        for surface_type in self.surfaces:
            area += surface_type.area(self)
            return area
            
class Cube(Square, CalculateAreaMixin):
    def __init__(self, length):
        super().__init__(length)
        self.surfaces = [Square, Square, Square]
        
        
class RightPyramid(Square, Triangle, SurfaceAreaMixin):
    def __init__(self, base, slant_height):
        self.base = base
        self.slant_height = slant_height
        self.height = slant_height
        self.length = base
        self.width = base

        self.surfaces = [Square, Triangle, Triangle, Triangle, Triangle]
Output

>>> cube = Cube(3)
>>> cube.surface_area()
54

In the above example, we created a new Mixin called SurfaceAreaMixin. Mixin function does not care about its position or starting a new We can utilize the get freedom.

Concluding the article by comparing the multiple and multilevel inheritance.

Multiple vs Multilevel Inheritance

Multiple InheritanceMultilevel Inheritance
Multiple inheritances is a type of inheritance in which a class inherits from more than one base class.Multilevel inheritance is to inherit the class from a derived class, making that derived class the base class for this new class.
It is not widely used.It is widely used.
It has two class levels that are base class and derived class.It has three class levels:1- Base class, 2- Intermediate class 3- Derived class.

Categorized in: