Multi Level Inheritance in Python

In this article we want to learn about Multi Level Inheritance in Python, so in object oriented programming when you inherits a derived class from another derived class this is called multi level inheritance and it can be done at any depth. 

 

 

Python Multi Level Inheritance 

Multi-level inheritance in Python refers to the concept of creating a hierarchy of classes where each subclass inherits from its immediate parent class, and in turn, becomes the parent class for its own subclasses. This creates a chain of inheritance with multiple levels.

 

 

Now let’s take a look at this image.

Multi Level Inheritance in Python
Multi Level Inheritance in Python

 

If you see in the above image we have three classes, we have a Class A, this class is our base class. we have another Class B, this class extends from the base Class A. we have another Class C, now this class extends from Class B. as we have already said when you extends a derived class from another derived class this is called multi level inheritance and it can be done at any depth. now our Class B can access to all attributes and method of the Class A, and our Class C can access to all attributes and methods of the Class A and Class B.

 

 

Now let’s create a practical example, in this example we have our base class that is class Person, in this class we have created a method. we have two more classes, one is class Student and it is extending from class Person, there is a method in this class. also we have another class that is class Programmer, this class extends from the Student class. now if you create the instance of the Programmer class, this class can access to the method of the Person and Student classes.

 

 

 

Now run your code and this will be the result.

Multi Level Inheritance in Python
Multi Level Inheritance in Python

 

 

 

As i have already said you can create Python Multi Level Inheritance at any depth.

 

 

FAQs:

 

What is multi-level inheritance?

Multi-level inheritance is a type of inheritance in object-oriented programming where a class inherits from another class, which in turn inherits from another class, and it creates a hierarchy of classes. Each subclass inherits attributes and methods from its immediate parent class, as well as from all ancestor classes up the inheritance chain. This allows for code reuse and the organization of classes into a logical hierarchy.

 

 

Can Python do multiple inheritance?

Yes, Python supports multiple inheritance, it means that a class can inherit attributes and methods from multiple parent classes. This allows for greater flexibility in designing class hierarchies and enables code reuse.

 

 

What is an example of multiple inheritance?

This is an example of multiple inheritance in Python:

In this example, the MultiDerived class inherits from both Base1 and Base2, and it allows instances of MultiDerived to access methods from both parent classes.

 

 

What are types of inheritance in Python?

In Python, there are several types of inheritance:

    • Single Inheritance: A subclass inherits from only one parent class.
    • Multiple Inheritance: A subclass inherits from multiple parent classes.
    • Multi-level Inheritance: A subclass inherits from a parent class, which in turn inherits from another parent class.
    • Hierarchical Inheritance: Multiple subclasses inherit from a single parent class.
    • Hybrid Inheritance: Combination of multiple types of inheritance.

 

 

 

Learn More:

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Codeloop
Share via
Copy link
Powered by Social Snap
×