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.
Basic Syntax of Inheritance in Python:
class ParentClass:
# attributes and methods of the parent class
class ChildClass(ParentClass):
# additional attributes and methods of the child class
In this example, ChildClass is inheriting from ParentClass. The ChildClass can access the attributes and
methods of the ParentClass.
Types of Inheritance in Python:
1. Single Inheritance:
A child class inherits from only one parent class.
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:
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 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:
A child class can inherit from multiple parent classes.
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:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
return f"{self.brand} {self.model}"
class ElectricVehicle:
def __init__(self, battery_capacity):
self.battery_capacity = battery_capacity
def display_battery_info(self):
return f"Battery Capacity: {self.battery_capacity} kWh"
class Car(Vehicle, ElectricVehicle):
def __init__(self, brand, model, num_doors, battery_capacity):
# Calling the constructors of both parent classes
Vehicle.__init__(self, brand, model)
ElectricVehicle.__init__(self, battery_capacity)
self.num_doors = num_doors
def display_car_info(self):
return f"{self.brand} {self.model} - {self.num_doors} doors"
class HybridCar(Car):
def __init__(self, brand, model, num_doors, battery_capacity, fuel_type):
# Calling the constructor of the parent class (Car)
Car.__init__(self, brand, model, num_doors, battery_capacity)
self.fuel_type = fuel_type
def display_hybrid_info(self):
return f"{self.display_car_info()} - Fuel Type: {self.fuel_type}"
# Creating an instance
hybrid_car = HybridCar("Toyota", "Prius", 4, 1.5, "Petrol")
# Using methods
print(hybrid_car.display_info()) # Output: Toyota Prius
print(hybrid_car.display_car_info()) # Output: Toyota Prius - 4 doors
print(hybrid_car.display_battery_info()) # Output: Battery Capacity: 1.5 kWh
print(hybrid_car.display_hybrid_info()) # Output: Toyota Prius - 4 doors - Fuel Type: Petrol
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).