Search⌘ K
AI Features

Types of Inheritance

Explore the five key types of inheritance in Python object-oriented programming. Understand how parent and child classes relate through single, multi-level, hierarchical, multiple, and hybrid inheritance. Gain the skills to implement these inheritance types using practical code examples to improve code reusability and structure.

Based upon parent classes and child classes, there exists the following five types of inheritance:

  1. Single
  2. Multi-level
  3. Hierarchical
  4. Multiple
  5. Hybrid

Single inheritance

In single inheritance, there is only a single class extending from another class. We can take the example of the Vehicle class as the parent class, and the Car class as the child class. Let’s implement these classes below:

Python 3.5
class Vehicle: # parent class
def setTopSpeed(self, speed): # defining the set
self.topSpeed = speed
print("Top speed is set to", self.topSpeed)
class Car(Vehicle): # child class
def openTrunk(self):
print("Trunk is now open.")
corolla = Car() # creating an object of the Car class
corolla.setTopSpeed(220) # accessing methods from the parent class
corolla.openTrunk() # accessing method from its own class

Multi-level inheritance

When a class is derived from a class which itself is derived from another class, it is called multilevel inheritance. We can extend the classes to as many levels as we want to.

Let’s implement the three classes illustrated above:

  • A Car IS A Vehicle
  • A Hybrid IS A Car
Python 3.5
class Vehicle: # parent class
def setTopSpeed(self, speed): # defining the set
self.topSpeed = speed
print("Top speed is set to", self.topSpeed)
class Car(Vehicle): # child class of Vehicle
def openTrunk(self):
print("Trunk is now open.")
class Hybrid(Car): # child class of Car
def turnOnHybrid(self):
print("Hybrid mode is now switched on.")
priusPrime = Hybrid() # creating an object of the Hybrid class
priusPrime.setTopSpeed(220) # accessing methods from the parent class
priusPrime.openTrunk() # accessing method from the parent class
priusPrime.turnOnHybrid() # accessing method from the child class

Hierarchical inheritance

In hierarchical inheritance, more than one class extends, as per the requirement of the design, from the same base class. The common attributes of these child classes are implemented inside the base class.

Example:

  • A Car IS A Vehicle
  • A Truck IS A Vehicle

Below is a code example of hierarchical inheritance.

Python 3.5
class Vehicle: # parent class
def setTopSpeed(self, speed): # defining the set
self.topSpeed = speed
print("Top speed is set to", self.topSpeed)
class Car(Vehicle): # child class of Vehicle
pass
class Truck(Vehicle): # child class of Vehicle
pass
corolla = Car() # creating an object of the Car class
corolla.setTopSpeed(220) # accessing methods from the parent class
volvo = Truck() # creating an object of the Truck class
volvo.setTopSpeed(180) # accessing methods from the parent class

Multiple inheritance

When a class is derived from more than one base class, i.e., when a class has more than one immediate parent class, it is called multiple inheritance.

Example:

  • HybridEngine IS A ElectricEngine.
  • HybridEngine IS A CombustionEngine as well.

Below is a code example of multiple inheritance.

Python 3.5
class CombustionEngine():
def setTankCapacity(self, tankCapacity):
self.tankCapacity = tankCapacity
class ElectricEngine():
def setChargeCapacity(self, chargeCapacity):
self.chargeCapacity = chargeCapacity
# Child class inherited from CombustionEngine and ElectricEngine
class HybridEngine(CombustionEngine, ElectricEngine):
def printDetails(self):
print("Tank Capacity:", self.tankCapacity)
print("Charge Capacity:", self.chargeCapacity)
car = HybridEngine()
car.setChargeCapacity("250 W")
car.setTankCapacity("20 Litres")
car.printDetails()

Hybrid inheritance

A type of inheritance which is a combination of Multiple and Multi-level inheritance is called hybrid inheritance.

  • CombustionEngine IS A Engine.
  • ElectricEngine IS A Engine.
  • HybridEngine IS A ElectricEngine and a CombustionEngine.

Below is the code implementation of an example of hybrid inheritance.

Python 3.5
class Engine: # Parent class
def setPower(self, power):
self.power = power
class CombustionEngine(Engine): # Child class inherited from Engine
def setTankCapacity(self, tankCapacity):
self.tankCapacity = tankCapacity
class ElectricEngine(Engine): # Child class inherited from Engine
def setChargeCapacity(self, chargeCapacity):
self.chargeCapacity = chargeCapacity
# Child class inherited from CombustionEngine and ElectricEngine
class HybridEngine(CombustionEngine, ElectricEngine):
def printDetails(self):
print("Power:", self.power)
print("Tank Capacity:", self.tankCapacity)
print("Charge Capacity:", self.chargeCapacity)
car = HybridEngine()
car.setPower("2000 CC")
car.setChargeCapacity("250 W")
car.setTankCapacity("20 Litres")
car.printDetails()