Python Object & Class

Python Multiple Inheritance: What is it and How to use it?

In this article, you will learn about Python Multiple Inheritance and also about Multi-level inheritance and MRO along with examples for each.

Python Multiple Inheritance

Inheritance is the mechanism that helps in achieving the re-usability of the code. The child class can derive the properties of its parent class and all the subclasses of the parent class.

When a class is derived from more than one base class, it is called multiple inheritance. The child class inherits features of all the base classes from which it is inherited.

Syntax:

Class Base1:

Body of Base1 class

Class Base2:

Body of Base2 class

Class Derived (Base1, Base2):

Body of derived class

Python Multilevel Inheritance

A child and grandchild relationship is formed when a class is inherited from the derived class. This is called Multilevel Inheritance. It can be to any depth in python.

In Multilevel Inheritance, the newly derived class inherits features of both the parent class and the class from which the parent class is derived.

Example:

                    

# A Python program to demonstrate Multilevel inheritance

# Base or Super class.

class Base(object):

# Constructor

def __init__(self, name):

self.name = name

# To get name

def get_name(self):

return self.name

# Inherited or Subclass

class Child(Base)

# Constructor

def __init__(self, name, age):

Base.__init__(self, name)

self.age = age

# To get Age

def get_age(self):

return self.age

# Inherited or Subclass

class GrandChild(Child):

# Constructor

def __init__(self, name, age, address):

Child.__init__(self, name, age)

self.address = address

# To get address

def get_address(self):

return self.address

# Driver code

g = GrandChild("Apoyo", 21, "Roorkee")

print(g.get_name(), g.get_age(), g.get_address())

Output:

Apoyo 21 Roorkee

 

Explanation:

Here in the above example, the newly derived class GrandChild has inherited features of both the Base class and the Child class. It inherited ‘name’ from Base class and ‘Age’ from Child class.

Method Resolution Order in Python

Object class is the most base type class in Python, that is, all the other built-in or user-defined classes are derived from object class and all their objects are instances of the object class.

Example: The following code shows that list is a subclass of object class and integers and strings are instances of the object class.

Code:

                    

print(issubclass(list,object))

print(isinstance(5.5,object))

print(isinstance(“Hello”,object))

Output

True

True

True

Method Resolution Order(MRO) is used to denote a way of resolving a method or attribute. In the case of multiple inheritance, method resolution order defines the order in which the base classes are searched during the execution of a method.

At first, the method or an attribute is searched within the current class and if not found in this current class then the search continues into parent classes in depth-first, left, right manner ensuring none of the class is searched twice. This order is also known as a linearization of MultiDerived Class and the set of rules is called Method Resolution Order(MRO).

Methods are being called via an instance as the interpreter needs a way to resolve methods while inheriting from another class.

For example: Suppose we have a class A and class B, derived from class A and we have a method with the same name in both the classes:

Code:

                    

# Python program showing how MRO works

class A:

def Sample_Method(self):

print(” Class A method is invoked”)

class B(A):

def Sample_Method(self):

print(” Class B method is invoked”)

r = B()

r.Sample_Method()

Output

Class B method is invoked

Explanation

In the above example, the method of class B is invoked due to MRO which means the order followed is class B -> class A

This example shows the order goes from class to parent classes.

Now let’s see another example, the case when the current class is inherited from two classes

Code:

                    

# Python program showing how MRO works

class A1:

def Sample_Method(self):

print(“Class A1 method is invoked”)

class B1(A1):

def Sample_Method(self):

print(” Class B1 method is invoked”)

class C1(A1):

def Sample_Method(self):

print(“Class C1 method is invoked”)

class D1(B1, C1):

pass

r = D1()

r.Sample_Method()

Output

Class B1 method is invoked

Explanation

The above example shows that for multiple inheritance MRO follows depth-first order to resolve the methods or attributes that are ordered the same as tuples or base classes.

The order for the above code will be:

Class D1 -> Class B1 -> Class C1 -> Class A1

We can view the MRO of a class as the __mro__ attribute that returns a tuple or the mro() method that returns a list. 

Frequently Asked Questions

Q.1. Is multiple inheritance possible in python?

Answer: Inheritance is the mechanism that helps in achieving the re-usability of the code. The child class can derive the properties of its parent class and all the subclasses of the parent class. When a class is derived from more than one base class, it is called multiple inheritance.

Unlike Java and C++, multiple inheritance is possible in python.

 Q.2. How are constructors called in multilevel inheritance?

Answer: In Multilevel Inheritance, the constructors are called in a particular order known as Method Resolution Order. At first, the method or an attribute is searched within the current class and if not found in this current class then the search continues into parent classes in depth-first, left, right manner ensuring none of the class is searched twice.

Code:

                    

class A1:

def __init__(self):

self.msge = “Constructor of class A1 is invoked”

def getName(self):

print(self.msge)

class B1(A1):

def __init__(self):

self.msge = “Constructor of class B1 is invoked”

def getName(self):

print(self.msge)

class C1(A1):

def __init__(self):

self.msge = “Constructor of class C1 is invoked”

def getName(self):

print(self.msge)

class D1(B1, C1):

pass

r = D1()

r.getName()

Output

The constructor of class B1 is invoked

Explanation

The above example shows that for multiple inheritance MRO follows depth-first order to call the constructors that are ordered the same as tuples or base classes.

The order for the above code will be:

Class D1 -> Class B1 -> Class C1 -> Class A1

 Q.3. What is the use of multiple inheritance?

Answer: Multiple inheritance is used when we need to use more than one totally orthogonal hierarchy simultaneously. It is useful when a subclass needs to combine multiple contracts and inherit some, or all. For example, for the class IndianStudent, we need to inherit from both the Indian Class and the Student Class. In such cases, we need to inherit class IndianStudent from both the classes which are called multiple inheritance.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Browse

Python Object & Class

Leave a Reply

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

Browse

Python Object & Class

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.