Inheritance
Inheritance
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to
inherit the properties and methods of another class. This promotes code reuse and helps create a
hierarchy of classes.
class ParentClass:
class ChildClass(ParentClass):
In this example, ChildClass is inheriting from ParentClass. The ChildClass can access the attributes and
methods of the ParentClass.
1. Single Inheritance:
Structure:
class Parent:
pass
class Child(Parent):
pass
Example:
Let's consider a scenario where we have a Vehicle class, and we want to create a
more specific class called Car that inherits from the Vehicle class.
class Vehicle:
self.brand = brand
self.model = model
def display_info(self):
class Car(Vehicle):
self.num_doors = num_doors
def display_car_info(self):
# Creating instances
# Using methods
In this example, the Car class inherits from the Vehicle class. The Car class has its own
__init__ method to handle the number of doors specifically for cars, and it overrides the
display_info method from the Vehicle class to include information about the number of
doors.
Note the use of super().__init__(brand, model) in the __init__ method of the Car class.
This is used to call the constructor of the parent class (Vehicle), ensuring that the
initialization for the brand and model is still performed.
2. Multiple Inheritance:
Structure:
class Parent1:
pass
class Parent2:
pass
class Child(Parent1, Parent2):
pass
Example:
Let's modify the example to demonstrate multiple inheritance. In this scenario, we'll
introduce another class called ElectricVehicle, and the Car class will inherit from both the
Vehicle class and the ElectricVehicle class.
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
return f"{self.brand} {self.model}"
class Car(Vehicle):
def __init__(self, brand, model, num_doors):
# Calling the constructor of the parent class
Vehicle.__init__(self, brand, model)
self.num_doors = num_doors
def display_car_info(self):
return f"{self.brand} {self.model} - {self.num_doors} doors"
# Creating instances
vehicle = Vehicle("Generic", "Model")
car = Car("Toyota", "Camry", 4)
# Using methods
print(vehicle.display_info()) # Output: Generic Model
print(car.display_car_info()) # Output: Toyota Camry - 4 doors
print(car.display_info()) # Output: Toyota Camry
In this example, the Car class inherits from both the Vehicle class and the ElectricVehicle
class. The Car class calls the constructors of both parent classes using Vehicle.__init__(self,
brand, model) and ElectricVehicle.__init__(self, battery_capacity).
This is an example of multiple inheritance, where a class inherits from more than one parent
class. The Car class can access methods and attributes from both the Vehicle class and the
ElectricVehicle class.
3. Multilevel Inheritance:
A class can inherit from another class, and then another class can inherit from the
second class.
Structure:
class Grandparent:
pass
class Parent(Grandparent):
pass
class Child(Parent):
pass
Example:
Let's modify the example to demonstrate multilevel inheritance. In this scenario, we'll introduce an
additional class called HybridCar, and the HybridCar class will inherit from the Car class, which, in turn,
inherits from both the Vehicle class and the ElectricVehicle class.
class Vehicle:
self.brand = brand
self.model = model
def display_info(self):
class ElectricVehicle:
self.battery_capacity = battery_capacity
def display_battery_info(self):
return f"Battery Capacity: {self.battery_capacity} kWh"
ElectricVehicle.__init__(self, battery_capacity)
self.num_doors = num_doors
def display_car_info(self):
class HybridCar(Car):
self.fuel_type = fuel_type
def display_hybrid_info(self):
# Creating an instance
In this example, the HybridCar class inherits from the Car class, which, in turn, inherits from both the
Vehicle class and the ElectricVehicle class. This is an example of multilevel inheritance, where a class is
derived from a class that is already a subclass. The HybridCar class can access methods and attributes
from all the ancestor classes (Vehicle, ElectricVehicle, and Car).