Open In App

Hierarchical Inheritance with Examples in Python

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In the world of object-oriented programming, inheritance is a powerful concept that allows one class to inherit the properties and behaviors of another. Hierarchical Inheritance is a specific form of inheritance in Python that involves a single base class with multiple derived classes. This article explores the concept of Hierarchical Inheritance, its syntax, advantages, and provides three examples to illustrate its application in Python.

What is Hierarchical Inheritance?

Hierarchical Inheritance is a type of inheritance in which a single base class is inherited by multiple derived classes. In this scenario, each derived class shares common attributes and methods from the same base class, forming a hierarchy of classes.

Syntax :

class BaseClass:
# Base class attributes and methods

class DerivedClass1(BaseClass):
# Additional attributes and methods specific to DerivedClass1

class DerivedClass2(BaseClass):
# Additional attributes and methods specific to DerivedClass2

Hierarchical Inheritance Examples In Python

Below, are the Hierarchical Inheritance With Examples In Python.

Example 1: Animal Hierarchy

This Python code showcases hierarchical inheritance with a base class `Animal` and two derived classes, `Dog` and `Cat`. The `Animal` class initializes a `name` attribute and has a placeholder method `speak`. Both `Dog` and `Cat` inherit from `Animal`, providing their own implementations of the `speak` method. Instances of `Dog` and `Cat` are created with specific names, and their `speak` methods are called to print the sounds they make. In the example, “Buddy says Woof!” and “Whiskers says Meow!” are printed.

Python3




class Animal:
    def __init__(self, name):
        self.name = name
 
    def speak(self):
        pass
 
class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"
 
class Cat(Animal):
    def speak(self):
        return f"{self.name} says Meow!"
 
# Usage
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) 
print(cat.speak())


Output

Buddy says Woof!
Whiskers says Meow!


Example 2: Shape Hierarchy

In this example, below Python code defines a basic example of hierarchical inheritance with a base class `Shape` and two derived classes, `Circle` and `Square`. The `Shape` class initializes a color attribute and has a placeholder method for calculating area. Both `Circle` and `Square` inherit from `Shape`, providing their own implementations of the `area` method. Instances of `Circle` and `Square` are created with specific color and dimension parameters, and their `area` methods are called to calculate and print the respective areas of a circle and a square.

Python3




class Shape:
    def __init__(self, color):
        self.color = color
 
    def area(self):
        pass
 
class Circle(Shape):
    def __init__(self, color, radius):
        super().__init__(color)
        self.radius = radius
 
    def area(self):
        return 3.14 * self.radius**2
 
class Square(Shape):
    def __init__(self, color, side_length):
        super().__init__(color)
        self.side_length = side_length
 
    def area(self):
        return self.side_length**2
 
# Usage
circle = Circle("Red", 5)
square = Square("Blue", 4)
print(circle.area())
print(square.area())


Output

78.5
16


Example 3: Employee Hierarchy

In this example, below Python code demonstrates hierarchical inheritance with a base class `Employee` and two derived classes, `Manager` and `Developer`. Each class initializes attributes and has a `display_details` method. The `Manager` class includes a `team_size` attribute, while the `Developer` class has a `programming_language` attribute. Instances of both classes are created, and their `display_details` methods are invoked to display specific information about a manager and a developer.

Python3




class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
 
    def display_details(self):
        return f"Name: {self.name}, Salary: {self.salary}"
 
class Manager(Employee):
    def __init__(self, name, salary, team_size):
        super().__init__(name, salary)
        self.team_size = team_size
 
    def display_details(self):
        return f"{super().display_details()}, Team Size: {self.team_size}"
 
class Developer(Employee):
    def __init__(self, name, salary, programming_language):
        super().__init__(name, salary)
        self.programming_language = programming_language
 
    def display_details(self):
        return f"{super().display_details()}, Language: {self.programming_language}"
 
# Usage
manager = Manager("John", 80000, 10)
developer = Developer("Alice", 60000, "Python")
print(manager.display_details())   
print(developer.display_details()) 


Output

Name: John, Salary: 80000, Team Size: 10
Name: Alice, Salary: 60000, Language: Python


Advantages

  • Code Reusability: Hierarchical Inheritance promotes code reusability by allowing multiple derived classes to inherit common functionalities from a single base class.
  • Logical Organization: It provides a logical and structured organization of classes, making the code more readable and maintainable.
  • Flexibility and Extensibility: Changes made to the base class automatically reflect in all derived classes, offering flexibility and ease of extending functionalities.
  • Reduced Redundancy: Common attributes and methods are defined only once in the base class, reducing redundancy and minimizing the chance of errors.

Conclusion

Hierarchical Inheritance in Python is a valuable feature that enhances code organization, reusability, and flexibility. By understanding its syntax, advantages, and exploring real-world examples, developers can make informed decisions on when and how to apply Hierarchical Inheritance in their projects, contributing to more efficient and maintainable code.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads