Python Multilevel Inheritance

Introduction

Multilevel inheritance is a type of inheritance in which a class (called the child or subclass) inherits from another class (called the parent or superclass), which in turn inherits from another class. This creates a chain of inheritance, where each class inherits attributes and methods from its predecessor.

Key Concepts

Grandparent Class

The top-most class in the inheritance chain.

Parent Class

The class that inherits from the grandparent class.

Child Class

The class that inherits from the parent class.

Multilevel Inheritance

Description: A class (child class) inherits from another class (parent class), which in turn inherits from another class (grandparent class).

 +---------+ | Class A | +---------+ | | +---------+ | Class B | +---------+ | | +---------+ | Class C | +---------+ 

Syntax

Defining a Grandparent Class

class GrandparentClass: def __init__(self, attribute1): self.attribute1 = attribute1 def method1(self): return "This is method1 from GrandparentClass" 

Defining a Parent Class

class ParentClass(GrandparentClass): def __init__(self, attribute1, attribute2): super().__init__(attribute1) # Inheriting from GrandparentClass self.attribute2 = attribute2 def method2(self): return "This is method2 from ParentClass" 

Defining a Child Class

class ChildClass(ParentClass): def __init__(self, attribute1, attribute2, attribute3): super().__init__(attribute1, attribute2) # Inheriting from ParentClass self.attribute3 = attribute3 def method3(self): return "This is method3 from ChildClass" 

Example

Let’s consider a real-world example of a family hierarchy, where the Grandparent, Parent, and Child classes represent three generations.

Grandparent Class: Grandparent

class Grandparent: def __init__(self, name): self.name = name def get_name(self): return f"Grandparent: {self.name}" 

Parent Class: Parent

class Parent(Grandparent): def __init__(self, name, occupation): super().__init__(name) # Inheriting from Grandparent self.occupation = occupation def get_occupation(self): return f"Occupation: {self.occupation}" 

Child Class: Child

class Child(Parent): def __init__(self, name, occupation, school): super().__init__(name, occupation) # Inheriting from Parent self.school = school def get_school(self): return f"School: {self.school}" 

Creating and Using Objects

# Creating an object of the Child class child = Child("Ramesh", "Engineer", "Greenwood High") # Displaying information using methods from all three classes print(child.get_name()) # Output: Grandparent: Ramesh print(child.get_occupation()) # Output: Occupation: Engineer print(child.get_school()) # Output: School: Greenwood High 

Explanation

  1. Grandparent Class (Grandparent): Defines an attribute (name) and a method (get_name).
  2. Parent Class (Parent): Inherits from Grandparent, adds an attribute (occupation), and a method (get_occupation).
  3. Child Class (Child): Inherits from Parent, adds an attribute (school), and a method (get_school).

Real-World Example: Company Hierarchy

Let’s consider a real-world example of a company hierarchy, where we have an Employee class, a Manager class that inherits from Employee, and a Director class that inherits from Manager.

Grandparent Class: Employee

class Employee: def __init__(self, name, employee_id): self.name = name self.employee_id = employee_id def get_details(self): return f"Employee Name: {self.name}, ID: {self.employee_id}" 

Parent Class: Manager

class Manager(Employee): def __init__(self, name, employee_id, department): super().__init__(name, employee_id) # Inheriting from Employee self.department = department def get_department(self): return f"Department: {self.department}" 

Child Class: Director

class Director(Manager): def __init__(self, name, employee_id, department, region): super().__init__(name, employee_id, department) # Inheriting from Manager self.region = region def get_region(self): return f"Region: {self.region}" 

Creating and Using Objects

# Creating an object of the Director class director = Director("Sita", "D001", "Engineering", "North America") # Displaying information using methods from all three classes print(director.get_details()) # Output: Employee Name: Sita, ID: D001 print(director.get_department()) # Output: Department: Engineering print(director.get_region()) # Output: Region: North America 

Explanation

  1. Grandparent Class (Employee): Defines common attributes (name, employee_id) and a method (get_details) for all employees.
  2. Parent Class (Manager): Inherits from Employee, adds an attribute (department), and a method (get_department).
  3. Child Class (Director): Inherits from Manager, adds an attribute (region), and a method (get_region).

Conclusion

Multilevel inheritance in Python allows a class to inherit attributes and methods from a parent class, which in turn inherits from another class. This creates a hierarchy of classes that promotes code reusability and logical structure. By understanding and utilizing multilevel inheritance, you can design more organized and maintainable object-oriented systems. The provided examples of a family hierarchy and a company hierarchy demonstrate how multilevel inheritance can be effectively applied in real-world scenarios.

Leave a Comment

Scroll to Top