Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

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 implementmultilevel 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 thebase class orparent class, the class in the middle is called thederived class orchild class, and the class at the bottom of the chain is called thesub-derived class orsub-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 classclass Network:    def connectivity(self):        return "Network connects"# Derived classclass Network_5G(Network):    def fast_connectivity(self):        return "5G Network provides superfast connectivity"# Sub-derived classclass 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_Airtelnetwork_object = Network_5G_Airtel()print(network_object.connectivity())        # Inherited from Network classprint(network_object.fast_connectivity())   # Inherited from Network_5G classprint(network_object.fast_and_stable_connectivity())   # Inherited from Network_5G_Airtel class

In this code, theNetwork_5G_Airtel class inherits the functions and attributes from theNetwork_5G class, which in turn inherits from theNetwork class. So theNetwork_5G_Airtel will have access to methods such asconnectivity()(fromNetwork),fast_connectivity()(fromNetwork_5G), andfast_and_stable_connectivity()(fromNetwork_5G_Airtel itself).

When you run the code, itsoutput will be −

Network connects5G Network provides superfast connectivityAirtel 5G network is fast and remains stable

MRO for Multilevel Inheritance in Python

Let's consider a scenario from the above example in which theNetwork_5G class also have a method namedconnectivity(). Now, What will happen if we call theconnectivity() method from the instance of theNetwork_5G_Airtel class?. This is where theMethod Resolution Order (MRO) comes into play.

Let's modify the previous example to include aconnectivity() method in theNetwork_5G class −

# Base classclass Network:    def connectivity(self):        return "Network connects"# Derived classclass Network_5G(Network):    def fast_connectivity(self):        return "5G Network provides superfast connectivity"    def connectivity(self):        return "5G Network connects faster"# Sub-derived classclass 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_Airtelobj1 = Network_5G_Airtel()print(obj1.connectivity())  # Inherited from Network class

Theoutput of the above code will be −

5G Network connects faster

This shows that theconnectivity() method from theNetwork_5G class is called instead of the one from theNetwork 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 themro() 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 anexample to illustrate method overriding in multilevel inheritance −

# Base classclass Vehicle:    def start(self):        return "Vehicle starts"# Derived classclass Car(Vehicle):    def start(self):        return "Car starts"# Sub-derived classclass SportsCar(Car):    def start(self):        return "Sports Car starts"# Creating an instance of SportsCarsports_car = SportsCar()print(sports_car.start())  # Calls the start method of SportsCar class

Theoutput 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.

Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp