Day 24 – Inheritance
Project: Build an “Animal Sound System” that demonstrates inheritance, overriding, and the use of super().
01. Learning Goal
By the end of this lesson, you will be able to:
- Understand the concept of inheritance in OOP
- Use superclasses (parents) and subclasses (children)
- Apply method overriding to customize behavior
- Use the
super()function to call parent constructors and methods - Build multi-level inheritance structures
02. Problem Scenario
You are designing a system for an animal shelter.
Different animals share common behaviors (like making sounds),
but each type of animal expresses them differently.
Inheritance allows us to reuse and extend parent class code.
03. Step 1 – What is Inheritance?
Inheritance allows a class (child) to reuse and extend another class (parent).
It reduces code duplication and makes code more organized.
class Animal:
def speak(self):
print("This animal makes a sound.")
class Dog(Animal):
def bark(self):
print("Woof!")
d = Dog()
d.speak() # Inherited from parent
d.bark() # Defined in child
04. Step 2 – Method Overriding
A child class can redefine a method from its parent.
This is called overriding.
class Animal:
def speak(self):
print("Generic animal sound.")
class Cat(Animal):
def speak(self): # Override
print("Meow!")
c = Cat()
c.speak() # Meow!
05. Step 3 – Using super()
super() allows a child class to call methods or constructors from its parent.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, student_id):
super().__init__(name) # Call parent constructor
self.student_id = student_id
s = Student("Sabin", "S123")
print(s.name, s.student_id) # Sabin S123
06. Step 4 – Multi-Level Inheritance
Inheritance can span across multiple layers.
class A:
def methodA(self):
print("Method from A")
class B(A):
def methodB(self):
print("Method from B")
class C(B):
def methodC(self):
print("Method from C")
obj = C()
obj.methodA()
obj.methodB()
obj.methodC()
07. Step 5 – Practice Examples
Example 1: Vehicle Inheritance
class Vehicle:
def move(self):
print("The vehicle moves.")
class Car(Vehicle):
def move(self): # Override
print("The car drives.")
v = Vehicle()
v.move()
c = Car()
c.move()
Output:
The vehicle moves.
The car drives.
Example 2: Shape Inheritance
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, w, h):
self.w = w
self.h = h
def area(self): # Override
return self.w * self.h
rect = Rectangle(4, 5)
print("Rectangle area:", rect.area())
Output:
Rectangle area: 20
08. Step 6 – Mini Project: Animal Sound System
Build a simple multi-level inheritance system for animals.
class Animal:
def speak(self):
print("Some generic animal sound.")
class Mammal(Animal):
def speak(self):
print("Mammal sound.")
class Dog(Mammal):
def speak(self):
print("Woof!")
class Cat(Mammal):
def speak(self):
print("Meow!")
animals = [Dog(), Cat(), Mammal()]
for a in animals:
a.speak()
Output:
Woof!
Meow!
Mammal sound.
09. Reflection
You have learned how to:
- Reuse code efficiently with inheritance
- Customize behavior using method overriding
- Use super() to call parent class logic
- Build multi-level hierarchies for scalable systems
Next → Day 25 – Encapsulation
Learn how to protect data and control access using private attributes and getter/setter methods.
Top comments (0)