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:
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:
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.
Multiple inheritance VS. Multilevel inheritance
Multiple inheritance | Multilevel inheritance |
|
|
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
Comments
Post a Comment