Updated September 20, 2023
Introduction to Multilevel Inheritance in Python
In python, Multilevel inheritance is one type of inheritance being used to inherit both base class and derived class features to the newly derived class when we inherit a derived class from a base class and another derived class from the previous derived class up to any extent of depth of classes in python is called multilevel inheritance. While inheriting the methods and members of both base class and derived class will be inherited to the newly derived class. By using inheritance, we can achieve the reusability of code, relationships between the classes. If we have three classes A, B, and C where A is the superclass (Parent), B is the subclass (child), and C is the subclass of B (Grandchild).
Syntax of Multilevel Inheritance
Let us say we have three classes with names as the base, derived1, and derived2. The base class has its members and functions as below:
class base: //member of base class // functions of base class Pass class derived1(base): // members of derived1 class + base class // functions of derived1 class + base class pass class derived2(derived1): // members of derived2 class + derived1 class + base class // functions of derived2 class + derived1 class + base class Pass
In the above class example, we can see that the class base is the base class and class derived is inherited from the class base and class derived2 is inherited from the class derived1.
How Multilevel Inheritance Works in Python?
In python, multilevel inheritance works as below. Let us explain multilevel inheritance with an example as below:
class Parent: def __init__(self,name): self.name = name def getName(self): return self.name class Child(Parent): def __init__(self,name,age): Parent.__init__(self,name) self.age = age def getAge(self): return self.age class Grandchild(Child): def __init__(self,name,age,location): Child.__init__(self,name,age) self.location=location def getLocation(self): return self.location gc = Grandchild("Srinivas",24,"Hyderabad") print(gc.getName(), gc.getAge(), gc.getLocation())
In the above multilevel inheritance example, we have three classes named as Parent, Child, and Grandchild. The parent class is a base class that has a constructor that is assigning a name, and a function getName(). We can get the name of the grandchild using this function. Derived class Child is inherited from the base class Parent with its members as a constructor which is using the base class constructor to assign the name and assigning age here. It has a function getAge() to get the age of the given children. Now this class has members has getName(), getAge() functions and its constructors. New derived class Grandchild is inherited from the previously derived class Child. New derived class has the features of both base class Parent and derived class Child functions and members as its features. The newly derived class has a constructor that is using previous class constructor, and function getLocation() to get the location of the given child.
In python, multilevel inheritance, if it needs to search a specific function, a member variable first it searches in the present class, then in the parent class of the current class, and finally, the base class like this order will be followed based on in-depth of classes. This type of resolving the order of class search is called MRO (Method resolution order) in python. It will maintain the local precedence order and maintains the resolution order of the members of the class. Multilevel inheritance is used to achieve code reusability, maintain relations between the classes to achieve object-oriented programming.
In the above program, we have created an object gc of class Grandchild with parameters as name, age, and location and output of the above program is as below:
Example of Multilevel Inheritance in Python
Let us have a look on different example mentioned below:
We can achieve multilevel inheritance using the super() function in python. super() function allows to refer to the parent class of current class explicitly as in inheritance subclasses are inherited from the superclass. super() functions enables us to implement single, multiple, multilevel inheritances easily.
Let us see an example of multilevel inheritance using super().
class xyz: def__init__(self): print("hey, I am initialized , xyz") def sub_xyz(self,b): print("Printing from class xyz:",b) class xyz1(xyz): def __init__(self): print("hey, I am initialized, xyz1") super().__init__() def sub_xyz(self,b): print("Printing from class xyz1:", b) super().sub_xyz(b+1) class xyz2(xyz1): def __init__(self): print("hey, I am initialized, xyz2") super().__init__() def sub_xyz(self,b): print("Printing from class xyz2:",b) super().sub_xyz(b+1) if __name__ == '__main__': ob =xyz2() ob.sub_xyz(10)
In the above example of multilevel inheritance using the super() function, we have created an object ob of the derived class xyz2() and calling the function sub_xyz() from class xyz2 which inherits both classes xyz, xyz1. In the output of the above example, you can clearly the order in which python is calling the classes and how it is resolving the relationship between the classes.
In the above example, we have three classes xyz, xyz1, and xyz2. Class xyz has a constructor with a print statement and a function sub_xyz() with a print function. Similarly, class xyz1 which is inherited from the class xyz has a constructor in which print statement and a super function with an initializer constructor, function sub_xyz() with a parameter b with a print statement and super() calling the sub-function sub_xyz with a parameter. Similarly, class xyz2 is derived from class xyz1 with all the member functions as similar to the previously derived class xyz1 with a constructor, super function with a constructor and a function sub_xyz() with super() function calling sub_xyz() with a parameter.
We have created an object ob of last derived class ab2 and called the function sub_xyz() with a parameter as 10 which calls the functions from derived class to base class in bottom-up order.
The output of the above program is:
Finally, it’s all about multilevel inheritance in python. We have discussed what is multilevel inheritance, its syntax, how multilevel inheritance works in python, and examples of multilevel inheritance using a super function. I hope after reading this article you will gain some more knowledge then what you have already and get a better understanding of multilevel inheritance and scenarios when to use.
This is a guide to Multilevel Inheritance in Python. Here we discuss an introduction to Multilevel Inheritance in Python along with working and respective examples. You may also have a look at the following articles to learn more –