- Learning Objectives
- Prerequisites
- Introduction
- What is Inheritance?
- Why is Inheritance Required?
- Parent Class
- Child Class
- Terminologies Used in Inheritance
- Types of Inheritance
- Method Resolution Order (MRO)
- super() Function
- Advantages of Inheritance
- Summary
After completing this chapter, you will be able to:
- Understand Inheritance.
- Explain why Inheritance is required.
- Differentiate Parent and Child classes.
- Learn all types of Inheritance supported in Python.
- Understand Method Resolution Order (MRO).
- Learn the purpose of the super() function.
- Implement Inheritance in real-world applications.
Before learning this chapter, you should know:
- Classes
- Objects
- Constructors
- Encapsulation
- Abstraction
One of the biggest advantages of Object-Oriented Programming is Code Reusability.
Suppose we already have a class that contains common properties and methods.
Instead of creating another class from scratch, we can reuse the existing class.
This concept is known as Inheritance.
Inheritance allows one class to acquire the properties and methods of another class.
This reduces duplicate code and makes programs easier to maintain.
Inheritance is the process by which one class acquires the properties and methods of another class.
The existing class is called the Parent Class (Base Class or Superclass).
The new class is called the Child Class (Derived Class or Subclass).
Acquiring the properties and methods of one class into another class is called Inheritance.
Imagine a company that has different types of employees.
Every employee has:
- Employee ID
- Employee Name
- Salary
Managers have additional information:
- Department
Developers have additional information:
- Programming Language
Instead of rewriting the common employee details in every class, we create a parent class called Employee and inherit it.
This avoids code duplication.
Consider a Vehicle.
Common properties:
- Brand
- Color
- Engine Number
Different vehicles have their own additional features.
Car
- Number of Doors
Bike
- Helmet Type
Bus
- Seating Capacity
Instead of rewriting the common properties, each class inherits them from the Vehicle class.
A Parent Class is the class whose properties and methods are inherited.
It is also called:
- Base Class
- Super Class
Example
class Person:
passA Child Class is the class that inherits from another class.
It is also called:
- Derived Class
- Sub Class
Example
class Student(Person):
passHere,
Person→ Parent ClassStudent→ Child Class
class Parent:
# Parent Members
class Child(Parent):
# Child MembersThe class whose members are inherited.
The class that inherits members.
A relationship between Parent and Child classes.
Using existing code instead of writing it again.
Inheritance represents an IS-A relationship.
Examples:
Car IS-A Vehicle
Dog IS-An Animal
Student IS-A Person
Python supports the following types of inheritance.
One Parent
↓
One Child
Parent
↓
Child
One Child inherits from multiple Parent classes.
Parent1
↘
Child
↗
Parent2
Inheritance continues through multiple levels.
GrandParent
↓
Parent
↓
Child
Multiple Child classes inherit from the same Parent.
Parent
↙ ↘
Child1 Child2
A combination of multiple inheritance types.
Python supports Hybrid Inheritance because it supports Multiple Inheritance.
When multiple inheritance is used,
Python must decide which method should execute first.
Python follows the Method Resolution Order (MRO).
You can check the MRO using:
ClassName.mro()or
help(ClassName)Python follows the C3 Linearization Algorithm to determine the MRO.
The super() function allows a child class to access the members of its parent class.
It is commonly used to:
- Call Parent Constructors
- Call Parent Methods
- Avoid duplicate code
Example
super().__init__()- Code Reusability
- Reduced Code Duplication
- Easy Maintenance
- Easy Extension
- Better Code Organization
- Faster Development
- Supports Hierarchical Design
- Tight coupling between classes.
- Changes in the parent class may affect child classes.
- Incorrect hierarchy can make code difficult to maintain.
Inheritance is used in:
- Banking Systems
- Employee Management Systems
- Hospital Management Systems
- School Management Systems
- Game Development
- GUI Frameworks
- Django Framework
- Machine Learning Libraries
Parent Class Created
↓
Child Class Inherits Parent
↓
Child Object Created
↓
Search Child Members
↓
If Not Found
↓
Search Parent Members
↓
Execute Method
In this part, we learned:
- Inheritance
- Need for Inheritance
- Parent Class
- Child Class
- Types of Inheritance
- IS-A Relationship
- Method Resolution Order (MRO)
- super() Function
- Advantages
- Internal Working
Write a Python program to demonstrate Single Inheritance.
class Person:
def display(self):
print("I am a Person")
class Student(Person):
def study(self):
print("Student is Studying")
student = Student()
student.display()
student.study()I am a Person
Student is Studying
Personis the Parent Class.Studentis the Child Class.- The child class inherits the
display()method from the parent class.
Write a Python program to demonstrate Multiple Inheritance.
class Father:
def father_property(self):
print("Father Property")
class Mother:
def mother_property(self):
print("Mother Property")
class Child(Father, Mother):
def child_property(self):
print("Child Property")
child = Child()
child.father_property()
child.mother_property()
child.child_property()Father Property
Mother Property
Child Property
The Child class inherits from both Father and Mother.
This is called Multiple Inheritance.
Write a Python program to demonstrate Multilevel Inheritance.
class GrandParent:
def grandparent(self):
print("Grand Parent")
class Parent(GrandParent):
def parent(self):
print("Parent")
class Child(Parent):
def child(self):
print("Child")
obj = Child()
obj.grandparent()
obj.parent()
obj.child()Grand Parent
Parent
Child
The inheritance chain is
GrandParent
↓
Parent
↓
Child
The child class inherits members from both its parent and grandparent.
Write a Python program to demonstrate Hierarchical Inheritance.
class Animal:
def eat(self):
print("Animal Eats")
class Dog(Animal):
def bark(self):
print("Dog Barks")
class Cat(Animal):
def meow(self):
print("Cat Meows")
dog = Dog()
cat = Cat()
dog.eat()
dog.bark()
cat.eat()
cat.meow()Animal Eats
Dog Barks
Animal Eats
Cat Meows
Both Dog and Cat inherit from the same parent class Animal.
Write a Python program to demonstrate the super() function.
class Person:
def display(self):
print("Person Class")
class Student(Person):
def display(self):
super().display()
print("Student Class")
student = Student()
student.display()Person Class
Student Class
super() calls the parent class method before executing the child class method.
Write a Python program to demonstrate Method Resolution Order.
class A:
def show(self):
print("Class A")
class B(A):
def show(self):
print("Class B")
class C(A):
def show(self):
print("Class C")
class D(B, C):
pass
obj = D()
obj.show()
print(D.mro())Class B
[<class '__main__.D'>,
<class '__main__.B'>,
<class '__main__.C'>,
<class '__main__.A'>,
<class 'object'>]
Python follows the Method Resolution Order (MRO) to determine which method should execute first.
The search order is
D
↓
B
↓
C
↓
A
↓
object
Since B appears before C, Python executes B.show().
Parent Class
▲
│
Child Class
▲
│
Child Object
The child object can access:
- Child Members
- Parent Members
Create Parent Class
↓
Create Child Class
↓
Child inherits Parent
↓
Create Child Object
↓
Search Child Method
↓
If not found
↓
Search Parent Method
↓
Execute Method
- Code Reusability
- Reduced Code Duplication
- Easy Maintenance
- Better Organization
- Faster Development
- Extensible Design
- Keep Parent Classes generic.
- Child Classes should extend, not duplicate.
- Prefer
super()over direct parent method calls. - Use inheritance only when an IS-A relationship exists.
- Avoid unnecessary deep inheritance hierarchies.
Using inheritance where there is no IS-A relationship.
❌ Incorrect
Car IS-A Engine
✅ Correct
Car HAS-A Engine
This is composition, not inheritance.
Forgetting to call the parent constructor.
Always use
super().__init__()when the parent constructor performs important initialization.
Ignoring Method Resolution Order (MRO) in Multiple Inheritance.
Understanding MRO helps avoid unexpected behavior.
Inheritance is widely used in:
- Banking Systems
- Hospital Management Systems
- School Management Systems
- E-Commerce Applications
- Game Development
- GUI Applications
- Django Framework
- Machine Learning Libraries
Inheritance is the process of acquiring the properties and methods of one class into another class.
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
A Parent Class is the class whose members are inherited by another class.
A Child Class is the class that inherits the members of a Parent Class.
super() is used to access the parent class's methods and constructors without directly referring to the parent class name.
Method Resolution Order (MRO) is the order in which Python searches classes for methods and attributes during inheritance.
Python uses the C3 Linearization Algorithm.
- Create a Person and Student class using Single Inheritance.
- Create Father, Mother, and Child classes using Multiple Inheritance.
- Demonstrate Multilevel Inheritance using three classes.
- Demonstrate Hierarchical Inheritance.
- Use
super()to call the parent method. - Display the MRO of a class using
mro().
- Inheritance enables code reuse.
- Parent Class → Base Class / Superclass.
- Child Class → Derived Class / Subclass.
- Inheritance represents an IS-A relationship.
- Python supports five types of inheritance.
super()accesses parent class members.- MRO determines the method lookup order.
In this chapter, you learned:
- Inheritance
- Parent and Child Classes
- Types of Inheritance
- IS-A Relationship
- Method Resolution Order (MRO)
super()Function- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision