Heyy, Ninjas, we are back with another Python-based article that will cover ‘Multilevel Inheritance in Python’ and how to use it in the program.
Python is a popular, easy-to-write general-purpose programming language among programmers. It is simple, has numerous applications, and is widely used in the Artificial Intelligence domain.
Before comprehending Multilevel Inheritance in Python, let's first take a quick look at the concept of inheritance itself.
Inheritance in Python
Inheritance is one of the crucial concepts in object-oriented programming (OOPs) methodology that helps us create a relationship between two classes. Let's understand the concept of Inheritance in Python using an example.
As you know, some of our habits and thinking match exactly with our parents. In Python, we have subclasses that inherit the properties of the parent classes. If the child class has another subclass, then it also inherits the properties and behaviours of the parent class as well as of that child class.
Another example of Inheritance could be that we have a class called Shape(parent class), and you create a new class from the base class, i.e., from the parent class called Circle(child class). The relationship between the two of them is Circle is a shape.
With the help of Inheritance, the child class or the derived class can access the properties and behaviours of the parent class or the base class.
Inheritance in OOPS allows programmers to create a new class from a pre-existing class, which supports code readability and reusability.
Know What is Object in OOPs here in detail.
Types of Inheritance in Python:
The types of Inheritance in Python are as follows:
- Single Inheritance in Python
- Multiple Inheritance in Python
- Multilevel Inheritance in Python
- Hierarchical Inheritance in Python
You can refer to types of inheritance in Python to learn more about them.
Let's get to know about multilevel Inheritance now.
Multilevel Inheritance in Python
Multilevel Inheritance in Python is a type of Inheritance that involves inheriting a class that has already inherited some other class. That means the derived/subclass class inherits the features of the base class/parent class, and the new derived class inherits the features of the derived class. As we discussed above, some of our habits match with our parents, and our parent's habits match with their parents, and this cycle continues. This is a simple relationship between a child and a grandfather.
Take a look at this image to understand the concept better:
In the above diagram, you can see, A is the grandparent class, B is the parent, and C is the child class. B is a subclass or child class of A, so it inherits the methods of A in it. Similarly, C is the child class of B, so it inherits all the methods associated with B and its parent. This is how multilevel inheritance is shown.
Also see, Merge Sort Python
The syntax of multilevel inheritance in python is as follows:
class base: // Members and Functions Pass class derived1(base): // Members and functions of both base and derived1 classes pass class derived2(derived1): // Members and functions of base class, derived1 class, and derived2 class. Pass
Also See, Intersection in Python
Read About, Divmod in Python
How Multilevel Inheritance Works in Python?
Multilevel inheritance in Python allows a class to inherit properties and methods from a class that is already inherited from another class. This means that a subclass can be derived from a parent class, and that subclass can be used as a parent class for another subclass. The subclasses inherit all the attributes and methods of their parent classes, allowing them to reuse and extend functionality. In Python, we can create a chain of inheritance by creating subclasses that inherit from other subclasses, resulting in a multilevel inheritance hierarchy. This allows us to create complex class structures that are easy to understand and maintain.
Let’s move to some examples for more clarity.
Let’s say we have three classes: Manager, Reviewer, and Writer.
class Manager: def final_review(self): print("Final Review") class Reviewer(Manager): def review(self): print("Reviewing...") class Writer(Reviewer): def writes(self): print("Writes the code") o = Writer() o.final_review() o.review() o.writes()
The above multilevel inheritance example has three classes: Manager, Reviewer, and Writer. The Manager class is a base class with a function, final_review(). The Reviewer class inherits the Manager class, and the Writer class inherits the Reviewer class.
The following code of python shows multilevel inheritance:
class Family: def show(self): print("My Family....") class Father(Family): name_father = "" def show1(self): print(self.name_father) class Child(Father): name_child = "" def show2(self): print("Father Name :",self.name_father) print("Child Name : ",self.name_child) o = Child() o.name_father = "Sumit" o.name_child = "Rajat" o.show() o.show2()
In the above-shown example, we have three classes: Family, Father, and Child. Father inherits the properties of the Family, Father inherits the properties of the Family, and Child inherits the properties of Father. Child class defines the function show2() to print the name of his own and of the father.
In this example of multilevel Inheritance in Python, we will use the super() function. It is useful when we want to avoid the base class explicitly. Like when we have a child class and want to refer to the parent class, we can do it using the super() function.
Let's use a similar kind of example just like above to understand better.
class Person: def __init__(self): print('Person - Hii') def age(self, a): print('Printing the age: ', a) class Father(Person): def __init__(self): print('Father - Hii') super().__init__() def age(self, a): print('Printing the age(Father): ', a) super().age(a - 1) class Mother(Father): def __init__(self): print('Mother - Hii') super().__init__() def age(self, a): print('Printing the age(Mother): ', a) super().age(a + 5) # Main function if __name__ == '__main__': o = Mother() o.age(30)
You can compile it with online python compiler.
Check out Escape Sequence in Python
In the above example, using the super() function, we have three classes: Person, Father, and Mother. Father inherits the properties of Person, which has a function with a print statement and a and a super function with a constructor, function age() with a parameter 'a' with a print statement, and super() calling the sub-function age with a parameter.
Similarly, class Mother is derived from class Father with all the member functions similar to the previously derived class Father with a constructor, super function with a constructor, and a function age() with super() function calling age() with a parameter.
Check out this article - String slicing in Python.
Must Read Invalid Syntax in Python.
Frequently Asked Questions
What is a multilevel inheritance in Python?
Multilevel inheritance in Python involves inheriting a class that has already inherited some other class.
Name the types of inheritance in Python.
The four types of inheritance supported by Python are single inheritance, multiple inheritance, multilevel inheritance, and hierarchical inheritance.
Is multilevel inheritance supported in Python language?
Yes, multilevel inheritance is supported in Python. In fact, Python supports all kinds of inheritance, like hybrid inheritance, also in which we implement more than one type of Inheritance.
What are the features of multilevel inheritance in Python?
The main feature for which we use multilevel inheritance in Python is readability and reusability. We don't have to write the same code in the child class; it can inherit the properties from the base class.
What do you mean by Method Resolution Order(MRO) in Python?
In multiple Inheritance in Python, if it needs to search for a specific function, then it follows an in-depth order, and this way of solving the order of class is known as Method Resolution Order(MRO) in Python.
Inheritance is a crucial concept in Object-Oriented Programming. In the above article, we have thoroughly discussed the concept of Multilevel Inheritance in Python, its syntax, and some examples.
Refer to more articles based on this.
- OOPs in Python
- Objects and Classes in Python
- OOPs Interview Questions
- Features of Object Oriented Programming
- Fibonacci Series in Python
Also, check out some Guided Paths on topics like Data Structure, Data Structure, Algorithms, and Competitive Programming, as well as some Contests and very helpful Interview Experiences and Interview Bundles only on Coding Ninjas Studio, brought to you by Industry Experts.