Inheritance Mastery

Learn to create hierarchical relationships and reuse code efficiently

Essential Concept #2
1
2
3
4

🎯 80/20 Focus: What Really Matters

Master these 3 inheritance patterns and you'll handle 80% of real-world scenarios: Base classes, Method overriding, and super() calls

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)
Car
Motorcycle
Truck
ElectricCar
SportsCar
SportBike

Each level inherits all properties and methods from levels above

Essential Python Implementation

class Vehicle: """ Base class (Parent) - defines common attributes and methods """ def __init__(self, brand, model, year): self.brand = brand self.model = model self.year = year self.is_running = False def start_engine(self): """Common method inherited by all vehicles""" self.is_running = True return f"{self.brand} {self.model} engine started!" def stop_engine(self): """Common method inherited by all vehicles""" self.is_running = False return f"{self.brand} {self.model} engine stopped!" def get_info(self): """Base implementation - can be overridden""" return f"{self.year} {self.brand} {self.model}" class Car(Vehicle): """ Child class that inherits from Vehicle """ def __init__(self, brand, model, year, doors): # Call parent constructor using super() super().__init__(brand, model, year) # Add car-specific attribute self.doors = doors def get_info(self): """Override parent method with car-specific info""" base_info = super().get_info() # Get parent's implementation return f"{base_info} - {self.doors} doors" def honk(self): """Car-specific method""" return f"{self.brand} {self.model} goes BEEP BEEP!" class Motorcycle(Vehicle): """ Another child class with different specialization """ def __init__(self, brand, model, year, engine_size): super().__init__(brand, model, year) self.engine_size = engine_size def get_info(self): """Override with motorcycle-specific info""" base_info = super().get_info() return f"{base_info} - {self.engine_size}cc" def wheelie(self): """Motorcycle-specific method""" return f"{self.brand} {self.model} does a wheelie!" class ElectricCar(Car): """ Multi-level inheritance: ElectricCar inherits from Car, which inherits from Vehicle """ 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): """Override to provide electric-specific behavior""" self.is_running = True return f"{self.brand} {self.model} electric motor activated silently!" def charge(self): """Electric car specific method""" self.charge_level = 100 return f"{self.brand} {self.model} fully charged!" def get_info(self): """Override with electric car info""" base_info = super().get_info() return f"{base_info} - {self.battery_capacity}kWh battery" # Usage Examples car = Car("Toyota", "Camry", 2023, 4) motorcycle = Motorcycle("Honda", "CBR600", 2023, 600) electric_car = ElectricCar("Tesla", "Model 3", 2023, 4, 75) # All inherit common methods from Vehicle 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.