Hierarchical Inheritance With Examples in Python
Hierarchical Inheritance With Examples in Python
Python
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.
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
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.
class Animal:
def init (self, name): self.name = name
# Usage
dog = Dog("Buddy") cat = Cat("Whiskers")
print(dog.speak())
print(cat.speak()) Output
Buddy says Woof!
Whiskers says Meow!
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.
class Shape:
def init (self, color): self.color = color
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
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.
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
Flexibilityand 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