Hierarchical Inheritance in Python

Published Sept. 7, 2023, 10:28 a.m.

Hierarchical Inheritance is a type of inheritance in Python where a single superclass has multiple subclasses that inherit from it. It allows the subclasses to reuse and extend the attributes and methods of the superclass. For example, if you have a superclass called Vehicle that defines some common features of vehicles, you can create subclasses called Car, Bike, and Bus that inherit from Vehicle and add some specific features of each vehicle.

Here is a textual diagram to illustrate Hierarchical Inheritance in Python:

🚗 Vehicle (Superclass) 🚗
┌────────────────────┐
│   name             │
│   start()          │
└────────────────────┘
         ^
         |
         |
         |
         |
         v
🚘 Car (Subclass) 🚘     🏍️ Bike (Subclass) 🏍️     🚌 Bus (Subclass) 🚌 
┌────────────────────┐   ┌────────────────────┐   ┌────────────────────┐ 
│   color            │   │   type             │   │   capacity         │ 
│   drive()          │   │   pedal()          │   │   stop()           │ 
└────────────────────┘   └────────────────────┘   └────────────────────┘ 

Explanation:

  • We define the Vehicle class, which represents general vehicle features, with a name attribute and a start method.
  • The Car class inherits from Vehicle and adds a color attribute and a drive method, which are specific features of cars.
  • The Bike class inherits from Vehicle and adds a type attribute and a pedal method, which are specific features of bikes.
  • The Bus class inherits from Vehicle and adds a capacity attribute and a stop method, which are specific features of buses.
  • We create objects of each subclass and demonstrate Hierarchical Inheritance by calling methods from different levels of the hierarchy.
  • Comments in the code provide explanations for each step.

This example shows how each subclass inherits and extends features from the superclass, creating a branched structure of classes in Python. 🐍

Here is an example of Hierarchical Inheritance in Python with code explanations:

# Define a superclass called Vehicle
class Vehicle:
    # The __init__ method is a special method that is called when an object is created
    # It takes the parameters self and name and assigns them to the object's attributes
    def __init__(self, name):
        self.name = name # The name attribute stores the name of the vehicle

    # The start method is a regular method that prints that the vehicle starts
    def start(self):
        print(self.name + " starts")

# Define a subclass called Car that inherits from Vehicle
class Car(Vehicle):
    # The __init__ method of the subclass overrides the __init__ method of the superclass
    # It takes the parameters self, name, and color and assigns them to the object's attributes
    def __init__(self, name, color):
        # To keep the inheritance of the superclass's __init__ method, we use the super() function
        # The super() function returns a reference to the superclass
        # We call the __init__ method of the superclass with the name parameter
        super().__init__(name)
        self.color = color # The color attribute stores the color of the car

    # The drive method is a new method that prints that the car drives
    def drive(self):
        print(self.name + " drives")

# Define another subclass called Bike that inherits from Vehicle
class Bike(Vehicle):
    # The __init__ method of the subclass overrides the __init__ method of the superclass
    # It takes the parameters self, name, and type and assigns them to the object's attributes
    def __init__(self, name, type):
        # To keep the inheritance of the superclass's __init__ method, we use the super() function
        # The super() function returns a reference to the superclass
        # We call the __init__ method of the superclass with the name parameter
        super().__init__(name)
        self.type = type # The type attribute stores the type of the bike

    # The pedal method is a new method that prints that the bike pedals
    def pedal(self):
        print(self.name + " pedals")

# Define another subclass called Bus that inherits from Vehicle
class Bus(Vehicle):
    # The __init__ method of the subclass overrides the __init__ method of the superclass
    # It takes the parameters self, name, and capacity and assigns them to the object's attributes
    def __init__(self, name, capacity):
        # To keep the inheritance of the superclass's __init__ method, we use the super() function
        # The super() function returns a reference to the superclass
        # We call the __init__ method of the superclass with the name parameter
        super().__init__(name)
        self.capacity = capacity # The capacity attribute stores the capacity of the bus

    # The stop method is a new method that prints that the bus stops
    def stop(self):
        print(self.name + " stops")

# Create objects of each subclass
car = Car("Ferrari", "red")
bike = Bike("Harley", "motorbike")
bus = Bus("Volvo", 50)

# Access methods from all levels of the inheritance hierarchy
car.start()  # This invokes the start method from Vehicle class
car.drive()  # This invokes drive method from Car class

bike.start()  # This invokes start method from Vehicle class
bike.pedal()  # This invokes pedal method from Bike class

bus.start()  # This invokes start method from Vehicle class
bus.stop()   # This invokes stop method from Bus class

Code Explanations:

  • The code defines a superclass called Vehicle and three subclasses called CarBike, and Bus that inherit from Vehicle.
  • The superclass Vehicle has an attribute called name that stores the name of the vehicle and a method called start that prints that the vehicle starts.
  • The subclass Car inherits the attribute and method from Vehicle and adds its own attribute called color that stores the color of the car and a method called drive that prints that the car drives.
  • The subclass Bike inherits the attribute and method from Vehicle and adds its own attribute called type that stores the type of the bike and a method called pedal that prints that the bike pedals.
  • The subclass Bus inherits the attribute and method from Vehicle and adds its own attribute called capacity that stores the capacity of the bus and a method called stop that prints that the bus stops.
  • The code creates objects of each subclass and demonstrates Hierarchical Inheritance by calling methods from different levels of the hierarchy.

I hope this helps you understand Hierarchical Inheritance in Python better. 😊