MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Multilevel Inheritance


Multiple inheritance and Multilevel inheritance are very similar concepts. If you have a complete understanding of multiple inheritance then understanding, multilevel will take no time. The minimum number of classes for Multilevel inheritance is the same as for multiple inheritance i.e., three.

What is Multilevel Inheritance in Python?


In multilevel inheritance, a class that is already derived from another class is derived by a third class. So in this way, the third class has all the other two former classes' features and functionalities. The syntax looks something like this:

class Parent1:
    pass
class Derived1(Parent1):
    pass
class Derived2(Derived1):
    pass

Now let us dive into the priority brought by the ordering of the class. Suppose that we are looking for a constructor or a value for any variable. Our program will first check our current class i.e., Derived2, for it. If nothing is found, then it will move towards Derived1 and in order at last towards Parent1 until it finds the constructor or variable in the way.

If we have the same method or variable in the base and derived class, then the order we discussed above will be followed, and the method will be overridden. Else, if the child class does not contain the same method, then the derived1 class method will be followed by the sequence defined in the paragraph above.

For Example


class level1:
      def first(self):
            print ('code')

class level2(level1): #inherit level1
      def second(self):
             print ('with')

class level3(level2): #inherit level2
      def third(self):
            print ('harry')

obj=level3()
obj.first()
obj.second()
obj.third()

Rules for Method overriding:-


There are few rules for Method overriding that should be followed:

The name of the child method should be the same as parents.
Inheritance should be there, and we need to derive a child class from a parent class
Both of their parameters should be the same.

Advantages of Inheritance


It reduces code redundancy.
Multilevel inheritance provides code reusability.
Using multilevel inheritance, code is easy to manage, and it supports code extensibility by overriding the base class functionality within child classes.

Example



class Dad:
    basketball =6

class Son(Dad):
    dance =1
    basketball = 9
    def isdance(self):
        return f"Yes I dance {self.dance} no of times"

class Grandson(Son):
    dance =6
    guitar = 1

    def isdance(self):
        return f"Jackson yeah!" \
            f"Yes I dance very awesomely {self.dance} no of times"

darry = Dad()
larry = Son()
harry = Grandson()

# print(darry.guitar)

# electronic device
# pocket gadget
# phone



Conclusion

In this page (written and validated by ) you learned about Python Multilevel Inheritance . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Public Private and Protected Access Specifiers.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.