Types of inheritance
As we have already learned about the Inheritance in detail in the previous article, In this article, we will learn about the types of inheritance. In Python, we have the following types of inheritance:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance

Single Inheritance
Single Inheritance is when a child class inherits from one parent class. In this type of inheritance, we have only one child and one parent.

class car:
def __init__(self, brand, seating_capacity):
self.brand = brand
self.seating_capacity = seating_capacity
def car_details(self):
print("This is a car.")
class electric_car(car):
def __init__(self, model, cartype, brand, seating_capacity):
car.__init__(self, brand, seating_capacity)
self.model = model
self.cartype = cartype
def elec_car_details(self):
print("This is an electric car.")
model_3 = electric_car("Model 3", "Sedan", "Tesla", 5)
model_3.model
model_3.car_details()
model_3.brand
Multiple Inheritance
When a derived class uses more than one base class, then it is said to be multiple inheritance.

Example:
class music_system:
def __init__(self, speakers):
self.speakers = speakers
def music_system_details(self):
print("This is a class for music system.")
class engine:
def __init__(self, engine):
self.engine = engine
def engine_details(self):
print("This is class for car engine.")
class car(music_system, engine):
def __init__(self, model, engine, speakers):
engine.__init__(self, engine)
music_system.__init__(self, speakers)
self.model = model
def car_details(self):
print("This is a class for car details.")
camry = car("Camry","Petrol",5)
camry.music_system_details()
camry.model
camry.speakers
Multilevel Inheritance
When a derived class is used to derive another class, then it is said to be multi-level inheritance. In simple words, we can say when we have a parent class, then child class and then grand child class.

Example:
class car:
def __init__(self, brand, seating_capacity):
self.brand = brand
self.seating_capacity = seating_capacity
def car_details(self):
print("This is a car.")
class electric_car(car):
def __init__(self, no_of_batteries, brand, seating_capacity):
car.__init__(self, brand, seating_capacity)
self.no_of_batteries = no_of_batteries
def elec_car_details(self):
print("This is an electric car.")
class electric_suv(electric_car):
def __init__(self, model, no_of_batteries, brand, seating_capacity):
electric_car.__init__(self, no_of_batteries, brand, seating_capacity)
self.model = model
def electric_suv(self):
print("This is electric SUV")
model_x = electric_suv("Model X", 10, "Tesla", 5)
model_x.brand
model_x.no_of_batteries
model_x.model
model_x.car_details()
Hierarchical Inheritance
When a single parent class has more than one child classes, this is called hierarchical inheritance.

class Car:
def __init__(self, brand, seating_capacity):
self.brand = brand
self.seating_capacity = seating_capacity
def car_details(self):
print("This is a car.")
class electric_Car(Car):
def __init__(self, no_of_batteries, brand, seating_capacity):
Car.__init__(self, brand, seating_capacity)
self.no_of_batteries = no_of_batteries
def car_details(self):
print("This is an electric car.")
class diesel_Car(Car):
def __init__(self, diesel_Capacity, brand, seating_capacity):
Car.__init__(self, brand, seating_capacity)
self.diesel_Capacity = diesel_Capacity
def car_details(self):
print("This is a diesel car.")
class petrol_Car(Car):
def __init__(self, petrol_Capacity, brand, seating_capacity):
Car.__init__(self, brand, seating_capacity)
self.petrol_Capacity = petrol_Capacity
def car_details(self):
print("This is a petrol car.")
corolla = diesel_Car(60,"Toyota", 4)
civic = petrol_Car(50, "Honda", 2)
print("Corolla seating capacity : ", corolla.seating_capacity)
corolla.car_details()
print("Civic seating capacity : ", civic.seating_capacity)
civic.car_details()
Hybrid Inheritance
The combination of multiple types of inheritance is known as hybrid inheritance.

class Car:
def __init__(self, brand, seating_capacity):
self.brand = brand
self.seating_capacity = seating_capacity
def car_details(self):
print("This is a car.")
class Electric_Car(Car):
def __init__(self, no_of_batteries, brand, seating_capacity):
Car.__init__(self, brand, seating_capacity)
self.no_of_batteries = no_of_batteries
def car_details(self):
print("This is an electric car.")
class Music_System:
def __init__(self, speakers):
self.speakers = speakers
def music_system_details(self):
print("This is a class for music system.")
class Sports_Car(Electric_Car, Music_System):
def __init__(self, no_of_batteries, brand, seating_capacity, speakers, top_speed):
Electric_Car.__init__(self, no_of_batteries, brand, seating_capacity)
Music_System.__init__(self, speakers)
self.top_speed = top_speed
def car_details(self):
print("This is a sports car")
mustang = Sports_Car(22, "Ford", 2, 5, 300)
print(mustang.top_speed)
print(mustang.speakers)
print(mustang.no_of_batteries)
mustang.car_details()
mustang.music_system_details()