Multiple Inheritance in Python
Python supports multiple inheritance, allowing a class to inherit from more than one parent class. This means a child class can access attributes and methods from multiple base classes, making it a powerful feature for code reusability and organization.
Syntax
class Parent1:
# Parent class code
class Parent2:
# Another parent class code
class Child(Parent1, Parent2):
# Child class inherits from both Parent1 and Parent2
How Multiple Inheritance Works
In multiple inheritance, a child class derives properties from multiple parent classes. The order of inheritance matters, as Python follows the Method Resolution Order (MRO) to determine which method to execute when there are name conflicts.
Examples
1. Basic Multiple Inheritance
Let’s start with a simple example where a child class inherits attributes from two parent classes.
In this example, we have two parent classes: Parent1
and Parent2
. The child class Child
inherits from both and can access their attributes and methods.
class Parent1:
def show1(self):
print("This is Parent1")
class Parent2:
def show2(self):
print("This is Parent2")
# Child class inheriting from both Parent1 and Parent2
class Child(Parent1, Parent2):
def show_child(self):
print("This is the Child class")
# Creating an instance of Child
obj = Child()
# Accessing methods from both parent classes
obj.show1()
obj.show2()
obj.show_child()
Output:
This is Parent1
This is Parent2
This is the Child class
Explanation:
- The class
Parent1
has a methodshow1()
, andParent2
hasshow2()
. - The
Child
class inherits both parents, meaning it can useshow1()
andshow2()
without defining them explicitly. - We create an instance of
Child
and call all methods to verify that inheritance works.
2. Method Resolution Order (MRO) in Multiple Inheritance
When multiple parent classes have methods with the same name, Python uses the Method Resolution Order (MRO) to determine which method to call.
class Parent1:
def show(self):
print("This is Parent1")
class Parent2:
def show(self):
print("This is Parent2")
# Child class inheriting from both Parent1 and Parent2
class Child(Parent1, Parent2):
pass
# Creating an instance of Child
obj = Child()
# Calling the show() method
obj.show()
Output:
This is Parent1
Explanation:
- Both
Parent1
andParent2
have a method calledshow()
. - The
Child
class inherits from both, but Python follows the MRO and prioritizesParent1
since it is listed first. - Thus, calling
obj.show()
executesshow()
fromParent1
, ignoringParent2
‘s method.
3. Handling Name Conflicts in Multiple Inheritance
To explicitly call a method from a specific parent, we use super()
or direct class reference.
class Parent1:
def show(self):
print("This is Parent1")
class Parent2:
def show(self):
print("This is Parent2")
# Child class explicitly calling Parent2's method
class Child(Parent1, Parent2):
def show(self):
print("Child class method")
Parent2.show(self) # Explicitly calling Parent2's show()
# Creating an instance of Child
obj = Child()
obj.show()
Output:
Child class method
This is Parent2
Explanation:
- The
show()
method inChild
first prints a message. - Then it explicitly calls
Parent2.show(self)
, ensuring thatParent2
‘s method executes. - This way, we control which parent’s method to execute, resolving conflicts.
4. Using super()
for Multiple Inheritance
Using super()
ensures that methods from all parent classes are called in a structured way.
class Parent1:
def show(self):
print("Parent1 method")
super().show()
class Parent2:
def show(self):
print("Parent2 method")
# Child class using super()
class Child(Parent1, Parent2):
def show(self):
print("Child method")
super().show()
# Creating an instance of Child
obj = Child()
obj.show()
Output:
Child method
Parent1 method
Parent2 method
Here, super()
ensures that methods are called in the correct order, following Python’s MRO.