Multiple Inheritance in Python (with Example)
When a class is derived from more than one base class, this types of inheritance is called multiple inheritance in Python. It allows a child class to inherit all the properties and methods from multiple parent classes.
In the multiple inheritance, there are two or more parent classes and one child class. Python supports multiple inheritance directly if we compare with multiple inheritance in Java programming language.
Java does not support multiple inheritance directly through classes but it supports multiple inheritance through interface.
An example of multiple inheritance has been shown in the below figure in which a child class inherits all the features of two parent classes.
In the above figure, a child class C inherits all the functionalities and features of both parent classes A and B.
Syntax to Define Multiple Inheritance in Python
In Python, we can define multiple inheritance by listing multiple parent classes in the class definition. The general syntax to define multiple inheritance in Python programming is as follows:
class Parent1:
# Parent1 class attributes and methods
class Parent2:
# Parent2 class attributes and methods
# ... Define more parent classes if needed ...
class ParentN:
# ParentN class attributes and methods
class ChildClass(Parent1, Parent2, ..., ParentN):
# ChildClass attributes and methods
In the multiple inheritance, we can include as many parent classes as needed, separated by commas. The ChildClass will inherit attributes and methods from all the listed parent classes.
Simple Multiple Inheritance Example Program
Let’s take some simple example programs based on the multiple inheritance in Python, which you must practice to clear the concept.
Example 1:
# Python program to demonstrate multiple inheritance.
# Create first parent class.
class Parent1:
def m1(self):
print("m1 method of Parent1 class")
# Create second parent class.
class Parent2:
def m2(self):
print("m2 method of Parent2 class")
# Create a child class that inherits methods from both parent classes.
class ChildClass(Parent1, Parent2):
def m3(self):
print("m3 method of ChildClass class")
# Outside class definition.
# Create an object of ChildClass class.
child = ChildClass()
# Calling all the methods of child class using reference variable child.
child.m1()
child.m2()
child.m3()
Output:
m1 method of Parent1 class
m2 method of Parent2 class
m3 method of ChildClass class
In this basic example, we have defined two parent classes, Parent1 and Parent2, and one child class, ChildClass, that inherits from both parent classes. The object of ChildClass child is able to access the contents of class Parent1 and class Parent2.
Example 2:
class Parent1:
def method1(self):
return "Method from Parent1"
class Parent2:
def method2(self):
return "Method from Parent2"
class ChildClass(Parent1, Parent2):
pass
# Creating an instance of ChildClass
child_obj = ChildClass()
# Accessing methods from parent classes
output1 = child_obj.method1()
output2 = child_obj.method2()
print(output1)
print(output2)
Output:
Method from Parent1
Method from Parent2
Example 3:
# Python program to design a simple calculator using multiple inheritance.
class Addition:
def add(self, x, y):
return x + y
class Subtration:
def sub(self, x, y):
return x - y
class Multiplication:
def multiply(self, x, y):
return x * y
class Calculation(Addition, Subtration, Multiplication):
def div(self, x, y):
return x / y
# Create an instance of class Calculation.
cal = Calculation()
# Calling methods of parent classes as well as child class.
result1 = cal.add(20, 10)
result2 = cal.sub(40, 20)
result3 = cal.multiply(10, 30)
result4 = cal.div(20, 5)
print(result1)
print(result2)
print(result3)
print(result4)
Output:
30
20
300
4.0
Advanced Multiple Inheritance Example Program
Let’s take some advanced example programs based on the multiple inheritance in Python for the best practice.
Example 4:
# Creating first parent class.
class Parent1:
def method(self):
return "Method from Parent1"
# Creating second parent class.
class Parent2:
def method(self):
return "Method from Parent2"
# Creating a child class that inherits from both parent classes.
class ChildClass(Parent1, Parent2):
def child_method(self):
# Calling methods from both parent classes using super() function.
parent1_output = super().method()
parent2_output = super().method()
return f"ChildClass: {parent1_output} and {parent2_output}"
# Creating an instance of ChildClass
child_obj = ChildClass()
# Accessing method of ChildClass class.
output = child_obj.child_method()
print(output)
Output:
ChildClass: Method from Parent1 and Method from Parent1
Example 5:
# Parent class 1.
class Student:
# Constructor method of parent class 1.
def __init__(self):
self.name = input("Enter the student name: ")
self.rollNo = int(input("Enter your roll no: "))
# Method of parent class 1.
def display(self):
print("\nName: ", self.name)
print("Roll no: ", self.rollNo)
# Parent class 2.
class Marks:
def __init__(self):
self.p = int(input("Enter marks of physics: "))
self.c = int(input("Enter marks of chemistry: "))
self.m = int(input("Enter marks of maths: "))
# Method of parent class 2.
def display_marks(self):
print("Marks of physics: ", self.p)
print("Marks of chemistry: ", self.c)
print("Marks of maths: ", self.m)
# Child class.
class Result(Student, Marks):
# Constructor method of child class.
def __init__(self):
Student.__init__(self)
Marks.__init__(self)
self.total_marks = self.p + self.c + self.m
self.per = self.total_marks * 100 / 300
# Method of child class.
def display_result(self):
print("Total marks: ", self.total_marks)
print("Percentage: ", self.per)
# Creating an instance of derived class.
obj = Result()
# Calling methods of derived class.
obj.display()
obj.display_marks()
obj.display_result()
Output:
Enter the student name: Deepak
Enter your roll no: 05
Enter marks of physics: 89
Enter marks of chemistry: 90
Enter marks of maths: 99
Name: Deepak
Roll no: 5
Marks of physics: 89
Marks of chemistry: 90
Marks of maths: 99
Total marks: 278
Percentage: 92.66666666666667
In this example code, we have created three classes, two parent class Student and Marks, and one child class Result. The child class Result inherits from both Student and Marks classes. The class Student contains two data members name and rollno of student and an instance method display().
The display() method prints the values of name and rollno. Another parent class Marks contains three data members p, c, and m representing the marks of three subjects, physics, chemistry, and maths, respectively. It also contains display_marks() method, which prints the marks of all three subjects.
Then, we have defined a child class Result, which inherits from both classes Student and Marks. This class contains two data members, total_marks, and per representing percentage. It also contains an instance method display_result() to display total marks and percentage of a student.
Outside the class definition, we have created an object of child class. As we created a child class object, Python immediately calls the __init__() constructor method of child class, which further calls constructors of parent classes Student and Marks. This constructor initializes the values of student name, rollno, and marks of three subjects.
Subsequently, the total marks and percentage are calculated inside the __init__() method of child class. Then, we have called display(), display_marks(), and display_result() methods of Student, Marks, and Result classes using reference variable obj. These methods display appropriate result or output on the console.
Note: In the above code, we have called the constructor method of parent classes using classname, such as Student.__init__(self) and Marks.__init__(self). We can also use super() function to call parent class constructor.
But with multiple inheritance, the super() function calls only __init__() constructor method of the firstly inherited parent class. To make the call to other parent class constructor method, we will have to use classname with __init__() method, as done in the above code. Look at the below code:
super().__init__()
Marks.__init__(self)
Example 6:
# Parent class 1.
class Pan_card:
# Constructor method of parent class 1.
def __init__(self):
self.pan_number = "PAN029192AB"
print("This is a Pan_card class")
# Parent class 2.
class Aadhar_card:
def __init__(self):
self.aadhar_number = "456789720013423"
print("This is a aadhar_card class")
# Child class.
class Bank_account_opening(Pan_card, Aadhar_card):
# Constructor method of child class.
def __init__(self):
# Calling constructor of parent class 1 using super() function.
super().__init__()
# Calling constructor of parent class 2 using class name.
Aadhar_card.__init__(self)
print("This is a bank_account_opening class")
# Method of child class.
def customer_info(self):
print("Pen_number: ", self.pan_number)
print("Aadhar_number: ", self.aadhar_number)
# Creating an instance of derived class.
bank_obj = Bank_account_opening()
# Calling methods of derived class.
bank_obj.customer_info()
Output:
This is a Pan_card class
This is a aadhar_card class
This is a bank_account_opening class
Pen_number: PAN029192AB
Aadhar_number: 456789720013423
In this example program, we have reused the attributes of Pan_card and Aadhar_card classes in the new class named Bank_account_opening.
Example 7:
# Creating first parent class.
class Parent1:
def method(self):
return "Method from Parent1"
# Creating second parent class.
class Parent2:
def method(self):
return "Method from Parent2"
# Creating child class that inherits from both parent classes.
class ChildClass(Parent1, Parent2):
def child_method(self):
return "Method in ChildClass"
# Creating an instance of ChildClass
child_obj = ChildClass()
# Accessing methods from parent classes.
output1 = child_obj.method() # This will call the method from Parent1
output2 = child_obj.child_method() # This will call the method from ChildClass.
print(output1)
print(output2)
Output:
Method from Parent1
Method in ChildClass
In this example, we have defined three classes: Parent1, Parent2, and ChildClass. The class Parent1 has a method() that returns a string, “Method from Parent1.” The class Parent2 has a method() that returns a string, “Method from Parent2.” The child class ChildClass inherits from both Parent1 and Parent2.
Outside the class definition, we have created an instance of ChildClass. We have then accessed methods from both parent classes and the child class. The returned results have assigned to the variables output1 and output2.
Since ChildClass class inherits from both Parent1 and Parent2 classes, Python follows a method resolution order (MRO). In this case, it prioritizes the method from Parent1, so output1 will contain the string “Method from Parent1.”
The child_method() method is defined in ChildClass and returns the string “Method in ChildClass.” Finally, we have displayed the values of output1 and output2 using the print statements.
Advantages of Multiple Inheritance in Python
There are the following advantages of using multiple inheritance feature in Python programming. They are as:
- Multiple inheritance allows a class to inherit properties and methods from multiple parent classes. This increases code reuse and minimizes redundancy in the code.
- With multiple inheritance, we can create complex class hierarchies that model the real-world relationships more accurately. This flexibility can lead to more intuitive and maintainable code structures.
- By breaking down functionality into separate classes and then combining them through multiple inheritance, we can create modular code structure. This code structure makes it easier to extend and modify the codebase without affecting the unrelated code segment.
Disadvantages of Multiple Inheritance
There are several disadvantages of using multiple inheritance in Python that you should keep in mind. They are:
- As the number of parent classes increases, code can become more complex and harder to read and understand.
- The Diamond Problem is a specific problem in multiple inheritance where ambiguity arises when a class inherits from two or more classes with a common ancestor. Resolving method or attribute conflicts in such cases can be complex and error-prone.
- When multiple parent classes define methods or attributes with the same name, it can lead to naming conflicts in the child class. This conflict can be solved by using techniques like method overriding or the super() function.
In this tutorial, we have discussed multiple inheritance in Python with the help of various example programs. Hope that you will have understood the basic concept of multiple inheritance and practiced all advanced programs. Stay tuned with the next where we will learn about encapsulation which is another object-oriented programming feature in Python.
Thanks for reading!!!





