Types of Inheritance in Python


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:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance
Types of 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.

Single Inheritance
In [1]:
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.")
In [2]:
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.")
In [3]:
model_3 = electric_car("Model 3", "Sedan", "Tesla", 5)
In [4]:
model_3.model
Out[4]:
'Model 3'
In [5]:
model_3.car_details()
This is a car.
In [6]:
model_3.brand
Out[6]:
'Tesla'

Multiple Inheritance

When a derived class uses more than one base class, then it is said to be multiple inheritance.

Multiple Inheritance

Example:

In [1]:
class music_system:
    def __init__(self, speakers):
        self.speakers = speakers
    
    def music_system_details(self):
        print("This is a class for music system.")
In [2]:
class engine:
    def __init__(self, engine):
        self.engine = engine
    
    def engine_details(self):
        print("This is class for car engine.")
In [3]:
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.")
In [4]:
camry = car("Camry","Petrol",5)
In [5]:
camry.music_system_details()
This is a class for music system.
In [6]:
camry.model
Out[6]:
'Camry'
In [7]:
camry.speakers
Out[7]:
5

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.

Multilevel Inheritance

Example:

In [1]:
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.")
In [2]:
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.")
In [3]:
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")
In [4]:
model_x = electric_suv("Model X", 10, "Tesla", 5)
In [5]:
model_x.brand
Out[5]:
'Tesla'
In [6]:
model_x.no_of_batteries
Out[6]:
10
In [7]:
model_x.model
Out[7]:
'Model X'
In [8]:
model_x.car_details()
This is a car.

Hierarchical Inheritance

When a single parent class has more than one child classes, this is called hierarchical inheritance.

Hierarchical Inheritance
In [1]:
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.")
In [2]:
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()
Corolla seating capacity :  4
This is a diesel car.
Civic seating capacity :  2
This is a petrol car.

Hybrid Inheritance

The combination of multiple types of inheritance is known as hybrid inheritance.

Hybrid Inheritance
In [1]:
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")
In [2]:
mustang = Sports_Car(22, "Ford", 2, 5, 300)
In [3]:
print(mustang.top_speed)
print(mustang.speakers)
print(mustang.no_of_batteries)
mustang.car_details()
mustang.music_system_details()
300
5
22
This is a sports car
This is a class for music system.

References

  1. Types of Inheritance