Introduction
Hierarchical inheritance is a type of inheritance where multiple child classes inherit from a single parent class. This allows different classes to share common functionality defined in the parent class, while also adding their own unique features.
Key Concepts
Parent Class (Superclass)
The class whose attributes and methods are inherited by multiple child classes.
Child Classes (Subclasses)
The classes that inherit attributes and methods from the parent class.
Hierarchical Inheritance Diagram
Description: Multiple classes (child classes) inherit from a single parent class.
+---------+
| Class A |
+---------+
/ \
/ \
+---------+ +---------+
| Class B | | Class C |
+---------+ +---------+
Syntax
Defining a Parent Class
class ParentClass:
def __init__(self, attribute):
self.attribute = attribute
def method(self):
return "This is a method from ParentClass"
Defining Child Classes
class ChildClass1(ParentClass):
def __init__(self, attribute, attribute1):
super().__init__(attribute) # Inheriting from ParentClass
self.attribute1 = attribute1
def method1(self):
return "This is a method from ChildClass1"
class ChildClass2(ParentClass):
def __init__(self, attribute, attribute2):
super().__init__(attribute) # Inheriting from ParentClass
self.attribute2 = attribute2
def method2(self):
return "This is a method from ChildClass2"
Real-World Example: School System
Let’s consider a real-world example of a school system, where we have a Person
class as the parent class, and Teacher
and Student
classes as the child classes.
Parent Class: Person
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
return f"Name: {self.name}, Age: {self.age}"
Child Class: Teacher
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age) # Inheriting from Person
self.subject = subject
def display_info(self):
return f"{super().display_info()}, Subject: {self.subject}"
Child Class: Student
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age) # Inheriting from Person
self.grade = grade
def display_info(self):
return f"{super().display_info()}, Grade: {self.grade}"
Creating and Using Objects
# Creating objects of the Teacher and Student classes
teacher = Teacher("Mr. Sharma", 40, "Mathematics")
student = Student("Ravi", 16, "10th Grade")
# Displaying information using methods from all classes
print(teacher.display_info()) # Output: Name: Mr. Sharma, Age: 40, Subject: Mathematics
print(student.display_info()) # Output: Name: Ravi, Age: 16, Grade: 10th Grade
Explanation
- Parent Class (
Person
): Defines common attributes (name
,age
) and a method (display_info
) for all persons. - Child Class (
Teacher
): Inherits fromPerson
, adds an attribute (subject
), and overrides thedisplay_info
method to include the subject. - Child Class (
Student
): Inherits fromPerson
, adds an attribute (grade
), and overrides thedisplay_info
method to include the grade.
Conclusion
Hierarchical inheritance in Python allows multiple child classes to inherit attributes and methods from a single parent class. This promotes code reusability and logical structure, enabling the creation of more organized and maintainable object-oriented systems. The provided example of a school system demonstrates how hierarchical inheritance can be effectively applied in real-world scenarios.