Python Multiple Inheritance
1. What is Multiple Inheritance
Multiple inheritance occurs when a class inherits from more than one parent class.
class Father:
def skill1(self):
print("Driving")
class Mother:
def skill2(self):
print("Cooking")
class Child(Father, Mother):
pass
c = Child()
c.skill1()
c.skill2()The child class gains features from all parent classes.
2. Basic Multiple Inheritance Structure
class A:
def show_a(self):
print("Class A")
class B:
def show_b(self):
print("Class B")
class C(A, B):
pass
obj = C()
obj.show_a()
obj.show_b()The child class can access methods from both parents.
3. Overlapping Method Names
Python follows Method Resolution Order (MRO) to determine which method to invoke.
4. Understanding Method Resolution Order (MRO)
MRO defines the order of class traversal: C → A → B → object
5. Calling Parent Methods Explicitly
Allows execution of logic from multiple parents.
6. Multiple Inheritance with Constructors
Only the first parent’s constructor executes due to MRO.
7. Cooperative Multiple Inheritance Using super()
Demonstrates cooperative method chaining through MRO.
8. Diamond Problem Example
Python resolves this ambiguity using MRO, avoiding duplicate calls.
9. Visualizing the Inheritance Hierarchy
Helps understand order of class resolution for debugging.
10. Real-World Multiple Inheritance Example
Combines cross-cutting concerns such as logging and validation.
Last updated