Introduction π
**Multilevel Inheritance** is a type of inheritance where a class inherits from a parent class, and then another class inherits from that derived class. This forms a **chain of inheritance**.
Note
1. Basic Example of Multilevel Inheritance π§±
basic_multilevel.py
class GrandParent:
def feature1(self):
print("Feature 1 from GrandParent")
class Parent(GrandParent):
def feature2(self):
print("Feature 2 from Parent")
class Child(Parent):
def feature3(self):
print("Feature 3 from Child")
c = Child()
c.feature1()
c.feature2()
c.feature3()β Child gets access to both Parent and GrandParent methods
β Builds a chain of inherited behaviors
2. Constructor Chaining with super() π
constructor_chaining.py
class A:
def __init__(self):
print("A Constructor")
class B(A):
def __init__(self):
super().__init__()
print("B Constructor")
class C(B):
def __init__(self):
super().__init__()
print("C Constructor")
c = C()β Output calls constructors in order: A β B β C
β super() ensures proper constructor chaining
3. Overriding Methods in Multilevel Inheritance π
override_multilevel.py
class A:
def show(self):
print("A method")
class B(A):
def show(self):
print("B method")
class C(B):
def show(self):
print("C method")
c = C()
c.show()β The most derived class overrides all previous implementations.
4. Accessing Parent Methods from Child Using super() π
super_chain_example.py
class A:
def show(self):
print("A show")
class B(A):
def show(self):
print("B show")
super().show()
class C(B):
def show(self):
print("C show")
super().show()
c = C()
c.show()β Demonstrates the chain of execution
β Output: C show β B show β A show
5. Real-World Example β Company Hierarchy π¨βπΌ
company_hierarchy.py
class Company:
def company_info(self):
print("Company: Google")
class Department(Company):
def department_info(self):
print("Department: Development")
class Employee(Department):
def employee_info(self):
print("Employee: Sathish")
e = Employee()
e.company_info()
e.department_info()
e.employee_info()β Employee inherits features from Department and Company
6. Real-World Example β Vehicle System π
vehicle_system.py
class Vehicle:
def general_info(self):
print("Vehicles can move")
class Car(Vehicle):
def car_info(self):
print("Cars have 4 wheels")
class SportsCar(Car):
def sports_info(self):
print("Sports cars are fast")
s = SportsCar()
s.general_info()
s.car_info()
s.sports_info()7. Benefits of Multilevel Inheritance π
- β Promotes reusability across multiple layers
- β Allows extension of features step-by-step
- β Logical representation of hierarchical structures
- β Encourages clean code organization
8. Potential Issues β οΈ
- β Too many levels can make code complex
- β Debugging becomes harder across deep chains
- β Improper use of super() may cause issues
- β Overriding requires careful understanding of method order
9. Best Practices π‘
- β Keep inheritance chains short (2β3 levels max)
- β Use super() properly to avoid skipping parent constructors
- β Override only when needed
- β Consider composition if hierarchy becomes complicated
Conclusion π
You now fully understand Multilevel Inheritance in Python! Want the next topic? Try Hierarchical Inheritance, Hybrid Inheritance, MRO, or Constructor Overriding. Just tell me! π