What Is Multi-Level Inheritance in Python?

What is multi-level inheritance in Python?

Are you freshly learning and exploring multi-level inheritance in Python?

In Python, Multilevel inheritance is a type of inheritance that allows us to inherit the features of both the base class and the derived class. There aren’t any constraints upon the depth of inheritance you inherit as long as you need.

Multi-level inheritance in Python

 

We have different inheritance types in Python, including single, hierarchical, hybrid, multiple, and multilevel inheritance. But in this article, we’ll focus on multilevel inheritance in Python. To learn about the different types of inheritance that can be implemented with Python, you can refer to our more comprehensive guide here.

We’ll discuss the working and usefulness of it, so let’s begin! 😊

 

 

How Does Multi-Level Inheritance Work in Python?

As discussed in Multilevel inheritance, we can inherit the properties of a base class to a child class and a child class to a sub-child class. This concept is similar to the real-life inheritance concept like a son inherits property from his father, and the father inherits it from his father, the son’s grandfather, and so on.

To understand well, let’s see an example of multilevel inheritance in Python:

 

Code

# base class

class GrandFather:

    

    def __init__(self, grandfathername):

        self.grandfathername = grandfathername

        

        

# Father Class inherit GrandFather Class

class Father(GrandFather):

    def __init__(self, fathername, grandfathername):

        self.fathername = fathername

 

        # invokeing the constructor of Grandfather class

        GrandFather.__init__(self, grandfathername)

        




# Son class inheriting Father class, which is ultimatly inheriting GrandFahter class

class Son(Father):

    def __init__(self, sonname, fathername, grandfathername):

        self.sonname = sonname

 

        # invoking the constructor of Father class

        Father.__init__(self, fathername, grandfathername)

 

    def print_name(self):

        print('Grand father name :', self.grandfathername)

        print("Father name :", self.fathername)

        print("Son name :", self.sonname)

 

 

# create he object of class Son

s1 = Son('Tom', 'Bob', 'Sam')

s1.print_name()

 

Output

Grand father name : Sam

Father name : Bob

Son name : Tom

As you can see in the above example, I have tried to demonstrate the concept of multilevel inheritance in Python using a real-life use case of a grandfather and grandson inheritance relationship. The class Son has the authority to inherit the properties of the class GrandFather through the intermediate class Father

Other than that, this example has demonstrated the levels of inheritance, and in this case, it is two.

To check the order of inheritance, you can use MRO ( Method Resolution Order ) using the mro() function. The syntax of mro() is the quite a simple class name.mro(), so as an example, let’s check the MRO of class Son from the above example.

 

Code

Son.mro()

 

Output

[__main__.Son, __main__.Father, __main__.GrandFather, object]

 

Class Son is inheriting Father, and Father is inheriting GrandFather.

 

 

Conclusion

To summarize the article on Multilevel inheritance in Python, we have discussed what is multilevel inheritance in Python and how it works. Furthermore, we have discussed a real-life example of a grandson and grandfather relationship to demonstrate the implementation of this type of inheritance in Python.

Time to explore more. Comment below any scenario that best fits the concept of multilevel inheritance, except the parent-child use case.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share