Introduction π
**Hierarchical Inheritance** is a type of inheritance where **multiple child classes inherit from the same parent class**. This means one parent β many children, each gaining access to the parentβs attributes and methods.
Note
1. Basic Example of Hierarchical Inheritance π§±
basic_hierarchical.py
class Parent:
def feature(self):
print("Feature from Parent")
class Child1(Parent):
def child1_feature(self):
print("Feature from Child1")
class Child2(Parent):
def child2_feature(self):
print("Feature from Child2")
c1 = Child1()
c2 = Child2()
c1.feature()
c1.child1_feature()
c2.feature()
c2.child2_feature()β Both Child1 and Child2 inherit the parent method
β Each child has its own unique behavior
2. Overriding Methods in Children π
override_hierarchical.py
class Parent:
def greet(self):
print("Hello from Parent")
class Child1(Parent):
def greet(self):
print("Hello from Child1")
class Child2(Parent):
def greet(self):
print("Hello from Child2")
c1 = Child1()
c2 = Child2()
c1.greet()
c2.greet()β Each child class provides its own version of the parentβs method
β Demonstrates polymorphism
3. Constructors in Hierarchical Inheritance ποΈ
constructor_hierarchy.py
class Parent:
def __init__(self):
print("Parent Constructor")
class Child1(Parent):
def __init__(self):
super().__init__()
print("Child1 Constructor")
class Child2(Parent):
def __init__(self):
super().__init__()
print("Child2 Constructor")
c1 = Child1()
c2 = Child2()β Each child calls the parent constructor
β Maintains proper initialization across hierarchy
4. Real-World Example β Animal Classification πΎ
animals_example.py
class Animal:
def eat(self):
print("Eating...")
class Dog(Animal):
def bark(self):
print("Dog barks")
class Cat(Animal):
def meow(self):
print("Cat meows")
d = Dog()
c = Cat()
d.eat()
d.bark()
c.eat()
c.meow()β Both Dog and Cat inherit common behavior eat()
β Each has unique behavior
5. Real-World Example β E-Commerce Users π
ecommerce_example.py
class User:
def login(self):
print("User logged in")
class Buyer(User):
def purchase(self):
print("Buyer purchased a product")
class Seller(User):
def list_product(self):
print("Seller listed a product")
b = Buyer()
s = Seller()
b.login()
b.purchase()
s.login()
s.list_product()6. Hierarchical Inheritance with Shared & Unique Methods β¨
shared_unique.py
class Vehicle:
def start(self):
print("Vehicle starting...")
class Bike(Vehicle):
def wheels(self):
print("Bike has 2 wheels")
class Car(Vehicle):
def wheels(self):
print("Car has 4 wheels")
class Truck(Vehicle):
def wheels(self):
print("Truck has 6 wheels")
v = [Bike(), Car(), Truck()]
for veh in v:
veh.start()
veh.wheels()β Common functionality from parent
β Different behaviors in children
7. Benefits of Hierarchical Inheritance π
- β Reduces code duplication
- β Centralized control in parent class
- β Children inherit common logic automatically
- β Encourages consistent design
- β Makes extending classes easy
8. Challenges β οΈ
- β Too many child classes can cause clutter
- β Overriding without care may break consistency
- β Parent class changes affect all children
9. Best Practices π‘
- β Keep parent class generic & reusable
- β Let child classes define only unique behaviors
- β Avoid deep inheritance hierarchies
- β Use clear method names to avoid conflicts
Conclusion π
You now fully understand Hierarchical Inheritance in Python! Want the next topic? Try Hybrid Inheritance, MRO, Method Overriding, or Constructor Overriding. Just tell me! π