πŸ•ΈοΈ Python OOP β€” Multiple Inheritance

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

πŸ’‘ Multiple Inheritance = Child inherits from **two or more** parents
πŸ’‘ 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 πŸ’Ž

Media content

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 πŸŽ‰

>>β€œMultiple Inheritance gives Python classes the power to combine featuresβ€” but must be used wisely to keep code clean and maintainable.” ✨

You now fully understand Multiple Inheritance in Python! Want the next topic? Try Multilevel Inheritance, Hierarchical Inheritance, MRO, or Method Overriding. Just tell me! 😊