Introduction π
**Multiple Inheritance** allows a class to inherit from **more than one parent class**. This gives a child class access to **attributes and methods from multiple sources**, making it a powerful OOP feature.
Note
π‘ Syntax: class Child(Parent1, Parent2):
π‘ Used carefully to avoid ambiguity (diamond problem)
1. Basic Example of Multiple Inheritance π§±
basic_multiple_inheritance.py
class Father:
def skills(self):
print("Father: Driving")
class Mother:
def skills(self):
print("Mother: Cooking")
class Child(Father, Mother):
pass
c = Child()
c.skills()β Python uses **Method Resolution Order (MRO)**
β Here, Child inherits the skills() method from Father first
2. Calling Methods from Both Parents π
combine_skills.py
class Father:
def driving(self):
print("Driving skill")
class Mother:
def cooking(self):
print("Cooking skill")
class Child(Father, Mother):
pass
c = Child()
c.driving()
c.cooking()β Child class gains ALL non-conflicting parent methods.
3. Overriding Parent Methods in Child π
override_example.py
class A:
def show(self):
print("A class method")
class B:
def show(self):
print("B class method")
class C(A, B):
def show(self):
print("C class method")
c = C()
c.show()β Child class overrides both parent methods
β Overrides take highest priority
4. Using super() in Multiple Inheritance π§
Python's super() works based on MRO (Method Resolution Order). It doesn't simply call the parent β it follows the MRO chain.
super_mro.py
class A:
def show(self):
print("A method")
class B(A):
def show(self):
print("B method")
super().show()
class C(A):
def show(self):
print("C method")
super().show()
class D(B, C):
def show(self):
print("D method")
super().show()
d = D()
d.show()β Output follows MRO: D β B β C β A
β This solves the **Diamond Problem** elegantly
5. Diamond Problem Explained π

When two parent classes inherit from the same base class, and the child inherits from both β ambiguity arises. Python solves this using **C3 Linearization (MRO)**.
6. Check Class MRO π
mro_check.py
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
print(D.mro())β Shows the exact method lookup order Python follows.
7. Real-World Example β Smart Device System π±
smart_device.py
class Camera:
def take_photo(self):
print("Taking photo")
class MusicPlayer:
def play_music(self):
print("Playing music")
class Smartphone(Camera, MusicPlayer):
pass
s = Smartphone()
s.take_photo()
s.play_music()β Smartphone combines multiple capabilities through inheritance.
8. Real-World Example β Multiple Roles in a Job System π¨βπΌ
job_roles.py
class Manager:
def manage(self):
print("Managing team")
class Developer:
def code(self):
print("Writing code")
class TechLead(Manager, Developer):
pass
tl = TechLead()
tl.manage()
tl.code()9. Benefits of Multiple Inheritance π
- β Enables reusing features from multiple classes
- β Allows combining behaviors into one class
- β Reduces repeated code
- β Offers high flexibility in design
10. Challenges & Risks β οΈ
- β Method name conflicts between parents
- β Diamond problem (solved using MRO)
- β Harder to maintain and debug
- β Overuse can lead to confusing class structures
11. Best Practices π‘
- β Use multiple inheritance sparingly β only when needed
- β Prefer composition over inheritance in complex systems
- β Keep parent classes simple and focused
- β Always check MRO when using
super()
Conclusion π
You now fully understand Multiple Inheritance in Python! Want the next topic? Try Multilevel Inheritance, Hierarchical Inheritance, MRO, or Method Overriding. Just tell me! π