Multilevel Inheritance

Introduction

Multilevel Inheritance is a type of inheritance in Object-Oriented Programming (OOP) where a class inherits from another derived class. In other words, a child class becomes the parent of another class.

This creates a chain of inheritance where properties and methods are passed through multiple levels.

Multilevel inheritance is commonly used in:

  • Selenium Automation Frameworks

  • API Automation Frameworks

  • Web Applications

  • Enterprise Software

  • Banking Systems

  • Inventory Management Systems

  • Large-Scale Python Applications

Understanding multilevel inheritance helps developers create organized and reusable class hierarchies.

In this tutorial, you will learn what multilevel inheritance is, how it works, practical examples, automation testing use cases, common mistakes, and best practices.


What is Multilevel Inheritance?

Multilevel inheritance occurs when a class inherits from another derived class.

Structure

Grandparent Class
        ↓
 Parent Class
        ↓
 Child Class

Syntax

class Grandparent:
    pass

class Parent(Grandparent):
    pass

class Child(Parent):
    pass

The child class inherits features from both the parent and grandparent classes.


Why Use Multilevel Inheritance?

Multilevel inheritance helps:

  • Promote code reusability

  • Reduce duplication

  • Build hierarchical relationships

  • Improve maintainability

  • Support scalable application design


Basic Multilevel Inheritance Example

Example

class Grandparent:

    def house(self):
        print("Owns a House")

class Parent(Grandparent):

    def car(self):
        print("Owns a Car")

class Child(Parent):
    pass

child = Child()

child.house()
child.car()

Output

Owns a House
Owns a Car

The Child class can access methods from both parent levels.


Understanding the Inheritance Chain

Example

class Animal:

    def eat(self):
        print("Eating")

class Dog(Animal):

    def bark(self):
        print("Barking")

class Puppy(Dog):

    def play(self):
        print("Playing")

puppy = Puppy()

puppy.eat()
puppy.bark()
puppy.play()

Output

Eating
Barking
Playing

The Puppy class inherits from Dog, which inherits from Animal.


Inheriting Attributes

Attributes are inherited through every level.

Example

class Person:

    def __init__(self):
        self.name = "John"

class Employee(Person):
    pass

class Manager(Employee):
    pass

manager = Manager()

print(manager.name)

Output

John

The attribute is inherited from the top-level class.


Constructor Inheritance

Constructors can also be inherited.

Example

class Person:

    def __init__(self):
        print("Person Constructor")

class Employee(Person):
    pass

class Manager(Employee):
    pass

manager = Manager()

Output

Person Constructor

Using super() in Multilevel Inheritance

super() allows access to parent class methods and constructors.

Example

class Person:

    def __init__(self):
        print("Person Created")

class Employee(Person):

    def __init__(self):
        super().__init__()
        print("Employee Created")

class Manager(Employee):

    def __init__(self):
        super().__init__()
        print("Manager Created")

manager = Manager()

Output

Person Created
Employee Created
Manager Created

Method Overriding in Multilevel Inheritance

A child class can override inherited methods.

Example

class Animal:

    def sound(self):
        print("Animal Sound")

class Dog(Animal):

    def sound(self):
        print("Dog Bark")

class Puppy(Dog):
    pass

puppy = Puppy()

puppy.sound()

Output

Dog Bark

Python uses the nearest available method in the inheritance chain.


Method Resolution Order (MRO)

Python searches methods from the current class upward through the inheritance chain.

Example

class A:

    def show(self):
        print("Class A")

class B(A):
    pass

class C(B):
    pass

obj = C()

obj.show()

Output

Class A

Viewing MRO

Example

class A:
    pass

class B(A):
    pass

class C(B):
    pass

print(C.__mro__)

Output

(<class '__main__.C'>,
 <class '__main__.B'>,
 <class '__main__.A'>,
 <class 'object'>)

Real-World Example: Employee Hierarchy

Example

class Person:

    def show_name(self):
        print("John")

class Employee(Person):

    def show_department(self):
        print("IT")

class Manager(Employee):

    def manage_team(self):
        print("Managing Team")

manager = Manager()

manager.show_name()
manager.show_department()
manager.manage_team()

Output

John
IT
Managing Team

Real-World Example: Banking System

Example

class Account:

    def deposit(self):
        print("Deposit Successful")

class SavingsAccount(Account):

    def calculate_interest(self):
        print("Interest Calculated")

class PremiumSavingsAccount(
    SavingsAccount
):

    def premium_benefits(self):
        print("Premium Benefits Applied")

account = PremiumSavingsAccount()

account.deposit()
account.calculate_interest()
account.premium_benefits()

Output

Deposit Successful
Interest Calculated
Premium Benefits Applied

Multilevel Inheritance in Selenium Automation

Multilevel inheritance is commonly used in automation frameworks.


Example: Selenium Framework Structure

class BasePage:

    def open_browser(self):
        print("Browser Opened")

class LoginPage(BasePage):

    def login(self):
        print("Login Successful")

class DashboardPage(LoginPage):

    def open_dashboard(self):
        print("Dashboard Opened")

page = DashboardPage()

page.open_browser()
page.login()
page.open_dashboard()

Output

Browser Opened
Login Successful
Dashboard Opened

Example: Test Framework

class BaseTest:

    def setup(self):
        print("Driver Setup")

class LoginTest(BaseTest):

    def login(self):
        print("Login Executed")

class DashboardTest(LoginTest):

    def dashboard_test(self):
        print("Dashboard Tested")

test = DashboardTest()

test.setup()
test.login()
test.dashboard_test()

Output

Driver Setup
Login Executed
Dashboard Tested

Multilevel Inheritance in API Automation

Example

class APIBase:

    def send_request(self):
        print("Request Sent")

class UserAPI(APIBase):

    def get_user(self):
        print("User Retrieved")

class AdminAPI(UserAPI):

    def create_user(self):
        print("User Created")

api = AdminAPI()

api.send_request()
api.get_user()
api.create_user()

Output

Request Sent
User Retrieved
User Created

Common Mistakes Beginners Make

Creating Very Deep Inheritance Chains

Avoid:

A → B → C → D → E → F

Deep hierarchies become difficult to maintain.


Forgetting super()

Incorrect

class Employee(Person):

    def __init__(self):
        pass

Parent initialization is skipped.


Correct

class Employee(Person):

    def __init__(self):
        super().__init__()

Overriding Methods Unnecessarily

Only override methods when behavior needs to change.


Best Practices

Keep Inheritance Hierarchies Simple

Use only necessary levels.


Use super() for Constructors

super().__init__()

Reuse Common Functionality

Place shared code in higher-level classes.


Avoid Deep Nesting

Too many inheritance levels increase complexity.


Use Meaningful Class Names

Person
Employee
Manager

instead of:

A
B
C

Advantages of Multilevel Inheritance

  • Code reusability

  • Reduced duplication

  • Logical hierarchy

  • Easier maintenance

  • Scalable design


Limitations of Multilevel Inheritance

  • Can become complex

  • Deep hierarchies are difficult to debug

  • Changes in parent classes affect descendants

  • Overuse can reduce readability


Conclusion

Multilevel inheritance allows a class to inherit properties and methods through multiple levels of inheritance. It helps create logical hierarchies, reuse code efficiently, and reduce duplication.

Whether you’re developing Selenium automation frameworks, API testing solutions, banking systems, or enterprise applications, multilevel inheritance can help organize code in a scalable and maintainable manner.

Understanding multilevel inheritance is essential before learning more advanced OOP concepts such as hierarchical inheritance, polymorphism, abstraction, and design patterns.


Frequently Asked Questions (FAQs)

What is multilevel inheritance?

Multilevel inheritance occurs when a class inherits from another derived class.

Example:

class A:
    pass

class B(A):
    pass

class C(B):
    pass

Can a child class access grandparent methods?

Yes.

child.grandparent_method()

What is the inheritance chain?

Grandparent → Parent → Child

Why use super()?

super() helps call parent class constructors and methods.


Can methods be overridden in multilevel inheritance?

Yes.

Child classes can replace inherited methods with their own implementations.


Key Takeaways

  • Multilevel inheritance creates a chain of parent-child relationships.

  • Child classes inherit methods and attributes from all ancestor classes.

  • Constructors can be inherited and extended using super().

  • Method overriding works throughout the inheritance chain.

  • Python follows Method Resolution Order (MRO) when searching for methods.

  • Multilevel inheritance promotes code reuse and scalability.

  • Selenium and API automation frameworks frequently use multilevel inheritance.

  • Avoid excessively deep inheritance hierarchies.

  • Use meaningful class names and keep designs simple.

  • Understanding multilevel inheritance is essential for mastering Object-Oriented Programming in Python.