Multi Level Inheritance in Python



Inheritance is a feature in object-oriented programming through which a class can inherit attributes and methods from another class. In Python, you can implement different types of inheritance, such as single inheritance, multiple inheritance, and multilevel inheritance. This chapter covers how to implement multilevel inheritance in Python.

Multilevel Inheritance in Python

Multilevel inheritance is a type of inheritance that happens hierarchically such that a derived class inherits from another derived class. Meaning, a class is derived from a class which is also derived from another class. This forms a chain of inheritance.

The class at the top of the chain is called the base class or parent class, the class in the middle is called the derived class or child class, and the class at the bottom of the chain is called the sub-derived class or sub-child class.

The following illustrations depicts the concept of multilevel inheritance −

Multilevel Inheritance in Python

Example of Multilevel Inheritance in Python

Let's look at an example to understand how multilevel inheritance works in Python −

# Base class
class Network:
    def connectivity(self):
        return "Network connects"

# Derived class
class Network_5G(Network):
    def fast_connectivity(self):
        return "5G Network provides superfast connectivity"

# Sub-derived class
class Network_5G_Airtel(Network_5G):
    def fast_and_stable_connectivity(self):
        return "Airtel 5G network is fast and remains stable"    

# Creating an instance of Network_5G_Airtel
network_object = Network_5G_Airtel()
print(network_object.connectivity())        # Inherited from Network class
print(network_object.fast_connectivity())   # Inherited from Network_5G class
print(network_object.fast_and_stable_connectivity())   # Inherited from Network_5G_Airtel class

In this code, the Network_5G_Airtel class inherits the functions and attributes from the Network_5G class, which in turn inherits from the Network class. So the Network_5G_Airtel will have access to methods such as connectivity()(from Network), fast_connectivity()(from Network_5G), and fast_and_stable_connectivity()(from Network_5G_Airtel itself).

When you run the code, its output will be −

Network connects
5G Network provides superfast connectivity
Airtel 5G network is fast and remains stable

MRO for Multilevel Inheritance in Python

Let's consider a scenario from the above example in which the Network_5G class also have a method named connectivity(). Now, What will happen if we call the connectivity() method from the instance of the Network_5G_Airtel class?. This is where the Method Resolution Order (MRO) comes into play.

Let's modify the previous example to include a connectivity() method in the Network_5G class −

# Base class
class Network:
    def connectivity(self):
        return "Network connects"

# Derived class
class Network_5G(Network):
    def fast_connectivity(self):
        return "5G Network provides superfast connectivity"

    def connectivity(self):
        return "5G Network connects faster"

# Sub-derived class
class Network_5G_Airtel(Network_5G):
    def fast_and_stable_connectivity(self):
        return "Airtel 5G network is fast and remains stable"

# Creating an instance of Network_5G_Airtel
obj1 = Network_5G_Airtel()
print(obj1.connectivity())  # Inherited from Network class

The output of the above code will be −

5G Network connects faster

This shows that the connectivity() method from the Network_5G class is called instead of the one from the Network class. This is because the method resolution order (MRO) in Python follows a depth-first approach, meaning it will first look for the method in the current class, then in the parent class, and so on up the hierarchy.

To know the method resolution order of any class, you can use the mro() method. Here's how you can do it:

print(Network_5G_Airtel.mro())

Output:
[<class '__main__.Network_5G_Airtel'>, <class '__main__.Network_5G'>, 
<class '__main__.Network'>, <class 'object'>]

Overriding Methods in Multilevel Inheritance

In multilevel inheritance, a derived class can override methods of its parent class. Meaning, if a method is defined in both the parent class and the child class, the method in the child class will override the one in the parent class.

Here's an example to illustrate method overriding in multilevel inheritance −

# Base class
class Vehicle:
    def start(self):
        return "Vehicle starts"

# Derived class
class Car(Vehicle):
    def start(self):
        return "Car starts"

# Sub-derived class
class SportsCar(Car):
    def start(self):
        return "Sports Car starts"

# Creating an instance of SportsCar
sports_car = SportsCar()
print(sports_car.start())  # Calls the start method of SportsCar class

The output of the above code will be −

Sports Car starts

Conclusion

To conclude, multilevel inheritance is a type of inheritance where a class is derived from another derived class such that a chain of inheritance is formed. The method resolution order (MRO) will determine which method is called when there are methods with the same name in the inheritance chain. Method overriding can be performed to override the methods of the parent class in the child class.

Advertisements