Python Inheritance
Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) in Python. It allows a new class to reuse the properties and methods of an existing class instead of defining everything from the beginning.
Inheritance improves code reuse, modularity, and flexibility. A child class can access the attributes and methods of its parent class, and it can also extend or override inherited functionality to provide its own behavior.
In this tutorial, we will explore the basics of Python inheritance, different inheritance structures, multiple inheritance, method overriding, built-in functions such as issubclass() and isinstance(), and data hiding using private attributes.
Table of Contents

Basics of Python Inheritance
Inheritance allows one class to derive functionality from another class. The existing class is generally called the parent class or base class, while the new class is called the child class or derived class.
The derived class can use the functionality of the base class and can add its own methods or modify inherited methods.
Syntax
class DerivedClass(BaseClass):
<class-suite>
Python also supports multiple inheritance, where one class can inherit from more than one parent class.
Syntax for Multiple Inheritance
class DerivedClass(BaseClass1, BaseClass2, ..., BaseClassN):
<class-suite>
Single Inheritance in Python
Single inheritance occurs when one child class inherits from one parent class.
Example
class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal):
def bark(self):
print("Dog Barking")
d = Dog()
d.bark()
d.speak()
Output
Dog Barking
Animal Speaking
In this example, Animal is the parent class and Dog is the child class. The Dog class defines its own bark() method, while it also inherits the speak() method from Animal.
Multi-Level Inheritance
Multi-level inheritance creates a chain of inheritance. In this structure, one class inherits from another derived class.
Syntax
class Class1:
<class-suite>
class Class2(Class1):
<class-suite>
class Class3(Class2):
<class-suite>
Example
class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal):
def bark(self):
print("Dog Barking")
class DogChild(Dog):
def eat(self):
print("Eating Bread")
d = DogChild()
d.bark()
d.speak()
d.eat()
Output
Dog Barking
Animal Speaking
Eating Bread
Here, Dog inherits from Animal, and DogChild inherits from Dog. Therefore, the DogChild object can access methods available through both levels of inheritance.
Multiple Inheritance
In multiple inheritance, a single derived class inherits functionality from more than one parent class. This allows the derived class to use methods provided by multiple base classes.
Syntax
class Base1:
<class-suite>
class Base2:
<class-suite>
class Derived(Base1, Base2):
<class-suite>
Example
class Calculation1:
def add(self, a, b):
return a + b
class Calculation2:
def multiply(self, a, b):
return a * b
class Derived(Calculation1, Calculation2):
def divide(self, a, b):
return a / b
d = Derived()
print(d.add(10, 20))
print(d.multiply(10, 20))
print(d.divide(20, 10))
Output
30
200
2.0
The Derived class inherits from both Calculation1 and Calculation2. Therefore, its object can access the add(), multiply(), and divide() methods.
Useful Built-In Functions
issubclass()
The issubclass() function checks whether one class is a subclass of another class. It returns True when the relationship exists and False otherwise.
print(issubclass(Derived, Calculation1)) # True
print(issubclass(Calculation1, Calculation2)) # False
isinstance()
The isinstance() function checks whether an object belongs to a particular class.
print(isinstance(d, Derived)) # True
These functions can be useful when working with class relationships and objects in inheritance-based programs.
Method Overriding in Python
Method overriding occurs when a child class defines a method with the same name as a method in its parent class. The implementation provided by the child class is then used when that method is called through the child object.
Example
class Animal:
def speak(self):
print("Speaking")
class Dog(Animal):
def speak(self):
print("Barking")
d = Dog()
d.speak()
Output
Barking
In this example, both Animal and Dog contain a method named speak(). The implementation inside Dog overrides the implementation inherited from Animal.
Real-Life Example of Method Overriding
Method overriding can be demonstrated using different bank classes. The parent class provides a general interest rate, while individual bank classes provide their own values.
class Bank:
def get_interest_rate(self):
return 10
class SBI(Bank):
def get_interest_rate(self):
return 7
class ICICI(Bank):
def get_interest_rate(self):
return 8
b1 = Bank()
b2 = SBI()
b3 = ICICI()
print("Bank Rate of Interest:", b1.get_interest_rate())
print("SBI Rate of Interest:", b2.get_interest_rate())
print("ICICI Rate of Interest:", b3.get_interest_rate())
Output
Bank Rate of Interest: 10
SBI Rate of Interest: 7
ICICI Rate of Interest: 8
The SBI and ICICI classes override the get_interest_rate() method inherited from the Bank class and provide their own implementation.
Data Abstraction and Hiding
Python supports data hiding by using a double underscore prefix before an attribute name. Such an attribute is treated as private within the class and cannot normally be accessed directly using the same attribute name from outside the class.
Example
class Employee:
__count = 0
def __init__(self):
Employee.__count += 1
def display(self):
print("Number of employees:", Employee.__count)
emp1 = Employee()
emp2 = Employee()
try:
print(emp1.__count)
except AttributeError as e:
print(e)
finally:
emp1.display()
Output
'Employee' object has no attribute '__count'
Number of employees: 2
Here, __count is defined with a double underscore, making it a private attribute of the Employee class. The display() method provides access to its value from within the class.
Advantages of Inheritance in Python
- Code Reusability: Existing methods and attributes can be reused by derived classes.
- Modularity: Related functionality can be organized into parent and child classes.
- Flexibility: Child classes can extend or override inherited behavior.
- Maintainability: Common functionality can be kept in a base class rather than duplicated across multiple classes.
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
YT:- DecodeIT
Frequently Asked Questions
1. What is inheritance in Python?
Inheritance is an OOP concept that allows a child class to acquire properties and methods from a parent class.
2. What is single inheritance?
Single inheritance occurs when one child class inherits from one parent class.
3. What is multiple inheritance?
Multiple inheritance occurs when a single child class inherits from two or more parent classes.
4. What is multi-level inheritance?
Multi-level inheritance creates a chain in which a class inherits from another derived class.
5. What is method overriding?
Method overriding occurs when a child class provides its own implementation of a method that already exists in its parent class.
6. What does issubclass() do?
issubclass() checks whether one class is derived from another class and returns a Boolean result.
7. What does isinstance() do?
isinstance() checks whether an object is an instance of a specified class.
8. How is data hiding achieved in Python?
Data hiding can be implemented by prefixing an attribute with double underscores, such as __count.
Conclusion
Inheritance is a powerful feature of Python’s object-oriented programming system. It allows developers to reuse existing code, organize related classes, and create flexible application structures.
Python supports different inheritance structures, including single inheritance, multi-level inheritance, and multiple inheritance. Method overriding further allows child classes to customize inherited behavior, while functions such as issubclass() and isinstance() help work with class and object relationships.
Understanding inheritance is essential for building modular, reusable, and maintainable Python applications, especially when working with larger object-oriented projects.
Keywords: Python Inheritance, types of inheritance in Python, Python Inheritance,Python inheritance example, single inheritance in Python, multiple inheritance in Python, multi-level inheritance in Python, Python inheritance method overriding, method overriding in Python, Python inheritance super, inheritance in Python OOP, hierarchical inheritance in Python, polymorphism in Python, encapsulation in Python, Python OOP concepts,Python Inheritance