What is Inheritance?
Inheritance allows a class to inherit attributes and methods from another class.
It creates a parent-child relationship where the child class gets all the functionality
of the parent class and can add or modify behavior as needed.
Real-World Analogy
Think of inheritance like family traits. A child inherits characteristics from their parents
(eye color, height) but can also have their own unique features. In programming, a child class
inherits methods and attributes from a parent class but can add its own functionality.
Class Hierarchy Concept Map
Vehicle (Parent Class)
ElectricCar
SportsCar
SportBike
Each level inherits all properties and methods from levels above
Essential Python Implementation
class Vehicle:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
self.is_running = False
def start_engine(self):
self.is_running = True
return f"{self.brand} {self.model} engine started!"
def stop_engine(self):
self.is_running = False
return f"{self.brand} {self.model} engine stopped!"
def get_info(self):
return f"{self.year} {self.brand} {self.model}"
class Car(Vehicle):
def __init__(self, brand, model, year, doors):
super().__init__(brand, model, year)
self.doors = doors
def get_info(self):
base_info = super().get_info()
return f"{base_info} - {self.doors} doors"
def honk(self):
return f"{self.brand} {self.model} goes BEEP BEEP!"
class Motorcycle(Vehicle):
def __init__(self, brand, model, year, engine_size):
super().__init__(brand, model, year)
self.engine_size = engine_size
def get_info(self):
base_info = super().get_info()
return f"{base_info} - {self.engine_size}cc"
def wheelie(self):
return f"{self.brand} {self.model} does a wheelie!"
class ElectricCar(Car):
def __init__(self, brand, model, year, doors, battery_capacity):
super().__init__(brand, model, year, doors)
self.battery_capacity = battery_capacity
self.charge_level = 100
def start_engine(self):
self.is_running = True
return f"{self.brand} {self.model} electric motor activated silently!"
def charge(self):
self.charge_level = 100
return f"{self.brand} {self.model} fully charged!"
def get_info(self):
base_info = super().get_info()
return f"{base_info} - {self.battery_capacity}kWh battery"
car = Car("Toyota", "Camry", 2023, 4)
motorcycle = Motorcycle("Honda", "CBR600", 2023, 600)
electric_car = ElectricCar("Tesla", "Model 3", 2023, 4, 75)
vehicles = [car, motorcycle, electric_car]
for vehicle in vehicles:
print(vehicle.get_info())
print(vehicle.start_engine())
Key Benefits of Inheritance
- Code Reuse: Avoid duplicating common functionality
- Hierarchical Organization: Create logical relationships between classes
- Extensibility: Add new functionality while keeping existing code
- Polymorphism Support: Enables treating different objects uniformly
Key Insight
Inheritance creates an "is-a" relationship. A Car IS-A Vehicle, a Motorcycle IS-A Vehicle.
Use super() to call parent methods and avoid code duplication. Override methods when you need
specialized behavior for child classes.