Inheritance and MRO
Inheritance is one of the core pillars of Object-Oriented Programming (OOP) in Python. It allows a new class (child) to reuse, extend, and customize the behavior of an existing class (parent), promoting code reusability and logical organization.
Python supports multiple forms of inheritance and uses a well-defined mechanism called Method Resolution Order (MRO) to decide which method is executed when multiple classes define methods with the same name.
What is Inheritance?
- Inheritance allows a class to acquire methods and properties of another class
- The existing class is called the parent (base) class
- The new class is called the child (derived) class
- Child classes can access parent methods, but parents cannot access child methods

Types of Inheritance in Python
| Type | Description |
|---|---|
| Single-Level | One child inherits from one parent |
| Multi-Level | Parent → Child → Grandchild |
| Multiple | One child inherits from multiple parents |
Single-Level Inheritance
A child class inherits from a single parent class.

Example
class A:
def f1(self):
print("f1 works")
def f2(self):
print("f2 works")
class B(A):
def f3(self):
print("f3 works")
def f4(self):
print("f4 works")
obj = B()
obj.f1()Output
f1 worksExplanation
Ais the parent classBis the child classBinherits methodsf1()andf2()fromA
Access Rules in Inheritance
- A child class can access all parent methods
- A parent class cannot access child methods
- Sibling classes cannot access each other’s methods
Example
class C(A):
def f5(self):
print("f5 works")
obj = B()
obj.f5()Output
AttributeError: 'B' object has no attribute 'f5'Multi-Level Inheritance
A class inherits from another child class, forming a hierarchy.

Example
class A:
def f1(self):
print("f1 works")
def f2(self):
print("f2 works")
class B(A):
def f3(self):
print("f3 works")
def f4(self):
print("f4 works")
class C(A):
def f5(self):
print("f5 works")
obj = B()
obj.f4()Output
f4 works- Properties flow from grandparent to parent to child
- Commonly used for hierarchical relationships
Multiple Inheritance
A child class inherits from multiple parent classes.

Example
class A:
def show(self):
print("in A show")
class B:
def show(self):
print("in B show")
class C(A, B):
pass
obj = C()
obj.show()Output
in A showExplanation
- Both
AandBdefineshow() - Python selects the method based on MRO
Method Resolution Order (MRO)
What is MRO?
-
MRO defines the order in which Python searches classes for a method
-
Python searches:
- The child class
- Parent classes from left to right
- Grandparents (if any)
MRO Depends on Inheritance Order
Example
class C(B, A):
pass
obj = C()
obj.show()Output
in B show➡ Changing the order of parent classes changes the method selected.
Explicit Parent Method Call
If needed, you can directly call a specific parent’s method.
B.show(obj)Output
in B showThis bypasses MRO and calls the method explicitly.
How Python Resolves Method Conflicts?
Child class methods take highest priority, if not found, Python checks parent classes in MRO order

Summary
- Inheritance is a powerful feature in Python that enables structured code reuse and extensibility.
- Child classes can access parent methods, not vice versa.
- Python supports single-level, multi-level, and multiple inheritance.
- When multiple inheritance is used, Python relies on Method Resolution Order (MRO) to determine which method to execute, ensuring predictable and consistent behavior in complex class hierarchies.
Written By: Muskan Garg
How is this guide?
Last updated on
