- Learning Objectives
- Prerequisites
- Introduction
- What is Constructor Chaining?
- Why is Constructor Chaining Required?
- Types of Constructor Chaining
- Constructor Chaining using super()
- Constructor Chaining in Multilevel Inheritance
- Constructor Execution Order
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand Constructor Chaining.
- Explain the need for Constructor Chaining.
- Learn constructor execution order.
- Understand the role of
super(). - Implement Constructor Chaining in Single and Multilevel Inheritance.
- Write efficient constructor initialization code.
Before learning this chapter, you should know:
- Constructors
- Inheritance
- Method Overriding
super()Function
In Object-Oriented Programming,
a Child Class often inherits data and behavior from a Parent Class.
Both classes may define their own constructors.
When an object of the Child Class is created,
both constructors may need to execute to properly initialize the object.
The process of calling one constructor from another constructor is called Constructor Chaining.
Python provides Constructor Chaining using the super() function.
Constructor Chaining is the process of calling one constructor from another constructor.
In Python,
Constructor Chaining is commonly achieved using
super().__init__()This allows the Parent Class constructor to execute before the Child Class constructor continues.
Calling one constructor from another constructor is called Constructor Chaining.
Suppose we have a class
Person
containing
- Name
- Age
Now,
a class
Employee
inherits from Person and contains
- Employee ID
- Department
When an Employee object is created,
both the Person data and Employee data must be initialized.
Instead of rewriting the Parent Class initialization,
we call the Parent Constructor using
super().__init__()This avoids duplicate code.
Consider a University Management System.
Person
↓
Name
Age
Student
↓
Roll Number
Course
When a Student object is created,
the Parent constructor initializes
- Name
- Age
Then,
the Child constructor initializes
- Roll Number
- Course
This sequence is Constructor Chaining.
Python mainly supports Constructor Chaining through inheritance.
The common scenarios are:
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance (following MRO)
The super() function is used to call the Parent Class constructor.
Syntax
super().__init__()If the Parent constructor accepts parameters,
pass them through super().
Example
super().__init__(name, age)Suppose we have
GrandParent
↓
Parent
↓
Child
When the Child object is created,
constructors execute in the following order:
GrandParent
↓
Parent
↓
Child
Each constructor calls its Parent using super().__init__().
For Single Inheritance
Parent Constructor
↓
Child Constructor
For Multilevel Inheritance
GrandParent Constructor
↓
Parent Constructor
↓
Child Constructor
For Multiple Inheritance,
Python follows the Method Resolution Order (MRO).
Create Child Object
↓
Child Constructor Called
↓
super().__init__()
↓
Parent Constructor Called
↓
Parent Initialization Completed
↓
Control Returns
↓
Child Initialization Completed
- Avoids duplicate initialization code.
- Improves code reusability.
- Ensures proper object initialization.
- Simplifies inheritance.
- Improves maintainability.
Constructor Chaining is used in:
- Banking Systems
- Hospital Management Systems
- Employee Management Systems
- School Management Systems
- Django Models
- GUI Applications
- Enterprise Software
In this part, we learned:
- Constructor Chaining
- Need for Constructor Chaining
super()Function- Constructor Execution Order
- Constructor Chaining in Single Inheritance
- Constructor Chaining in Multilevel Inheritance
- Internal Working
Write a Python program to demonstrate Constructor Chaining using super().
class Person:
def __init__(self):
print("Person Constructor")
class Student(Person):
def __init__(self):
super().__init__()
print("Student Constructor")
student = Student()Person Constructor
Student Constructor
When the Student object is created:
Student.__init__()is called.super().__init__()invokes thePersonconstructor.- After the parent constructor finishes, execution returns to the child constructor.
This process is called Constructor Chaining.
Write a Python program to pass parameters from the Child constructor to the Parent constructor.
class Person:
def __init__(self, name):
self.name = name
print("Name :", self.name)
class Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = course
print("Course :", self.course)
student = Student("Rahul", "Python")Name : Rahul
Course : Python
The Child constructor forwards the name parameter to the Parent constructor using
super().__init__(name)The Child constructor then initializes its own data.
Write a Python program without using super().
class Person:
def __init__(self):
print("Person Constructor")
class Student(Person):
def __init__(self):
print("Student Constructor")
student = Student()Student Constructor
Since super().__init__() is not used,
the Parent constructor never executes.
As a result,
Parent class initialization is skipped.
Write a Python program to demonstrate Constructor Chaining in Multilevel Inheritance.
class GrandParent:
def __init__(self):
print("GrandParent Constructor")
class Parent(GrandParent):
def __init__(self):
super().__init__()
print("Parent Constructor")
class Child(Parent):
def __init__(self):
super().__init__()
print("Child Constructor")
child = Child()GrandParent Constructor
Parent Constructor
Child Constructor
Execution order:
GrandParent
↓
Parent
↓
Child
Each constructor calls its immediate parent using super().
class Employee:
def __init__(self, name):
self.name = name
class Manager(Employee):
def __init__(self, name, department):
super().__init__(name)
self.department = department
def display(self):
print("Name :", self.name)
print("Department :", self.department)
manager = Manager("Rahul", "HR")
manager.display()Name : Rahul
Department : HR
The Parent constructor initializes name.
The Child constructor initializes department.
Both classes participate in object initialization.
class A:
def __init__(self):
print("Class A")
class B(A):
def __init__(self):
super().__init__()
print("Class B")
class C(A):
def __init__(self):
super().__init__()
print("Class C")
class D(B, C):
def __init__(self):
super().__init__()
print("Class D")
d = D()Class A
Class C
Class B
Class D
Python follows the Method Resolution Order (MRO).
Execution Order
A
↓
C
↓
B
↓
D
You can verify it using
print(D.mro())class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
print(D.mro())[<class '__main__.D'>,
<class '__main__.B'>,
<class '__main__.C'>,
<class '__main__.A'>,
<class 'object'>]
The Method Resolution Order determines the sequence in which Python searches classes during inheritance.
class Person:
def __init__(self, name="Unknown"):
self.name = name
class Student(Person):
def __init__(self, name="Unknown", course="Python"):
super().__init__(name)
self.course = course
student = Student()
print(student.name)
print(student.course)Unknown
Python
Default arguments make constructor chaining more flexible.
class Employee:
def __init__(self):
print("Employee Initialized")
class Developer(Employee):
def __init__(self):
super().__init__()
print("Developer Initialized")
developer = Developer()Employee Initialized
Developer Initialized
class Parent:
def __init__(self):
print("Step 1 : Parent Constructor")
class Child(Parent):
def __init__(self):
print("Step 2 : Child Constructor Started")
super().__init__()
print("Step 3 : Child Constructor Completed")
child = Child()Step 2 : Child Constructor Started
Step 1 : Parent Constructor
Step 3 : Child Constructor Completed
Execution Flow
Child Constructor Starts
↓
Parent Constructor Executes
↓
Control Returns
↓
Child Constructor Completes
Create Child Object
↓
Child Constructor Starts
↓
super().__init__()
↓
Parent Constructor Executes
↓
Parent Initialization Complete
↓
Control Returns
↓
Child Initialization Complete
Child Object
│
▼
+----------------------------+
Parent Variables
↓
name
----------------------------
Child Variables
↓
course
+----------------------------+
- Eliminates duplicate initialization code.
- Promotes code reuse.
- Ensures proper initialization of inherited members.
- Simplifies inheritance.
- Improves readability and maintainability.
- Always call
super().__init__()when the Parent constructor initializes important data. - Pass required parameters through
super(). - Follow the MRO in Multiple Inheritance.
- Keep constructors focused on initialization only.
- Avoid unnecessary constructor logic.
Forgetting to call super().__init__().
This prevents Parent class initialization.
Passing incorrect arguments to super().
Always match the Parent constructor's parameters.
Calling the Parent constructor directly.
❌ Less Preferred
Person.__init__(self)✅ Preferred
super().__init__()Ignoring MRO in Multiple Inheritance.
Always understand the Method Resolution Order when using multiple parent classes.
Constructor Chaining is the process of calling one constructor from another constructor, usually using super().__init__().
It ensures that Parent class data is initialized before Child class initialization continues.
super().
The Parent constructor is not executed unless it is invoked explicitly.
Yes.
Each constructor calls its Parent constructor using super().
Yes.
In Multiple Inheritance, constructor calls follow the Method Resolution Order.
No.
Using super() is the recommended and more maintainable approach.
Yes.
Example:
super().__init__(name, age)Inheritance hierarchies such as Employee Management Systems, Banking Applications, School Management Systems, Django models, and GUI frameworks.
The Parent constructor executes before the Child constructor completes its initialization.
- Demonstrate Constructor Chaining using Single Inheritance.
- Demonstrate Constructor Chaining using Multilevel Inheritance.
- Pass parameters through
super(). - Display MRO using
mro(). - Create an Employee hierarchy using Constructor Chaining.
- Constructor Chaining calls one constructor from another.
super().__init__()invokes the Parent constructor.- Parent constructors should initialize inherited data.
- Multiple Inheritance follows MRO.
- Constructor Chaining improves code reuse and maintainability.
In this chapter, we learned:
- Constructor Chaining
super()Function- Constructor Execution Order
- Single Inheritance Constructor Chaining
- Multilevel Constructor Chaining
- Multiple Inheritance and MRO
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision