Inheritance in Python: Types, Best Practices & Examples
By Rohan Vats
Updated on Jun 30, 2025 | 12 min read | 20.13K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Jun 30, 2025 | 12 min read | 20.13K+ views
Share:
Did you know? In 2025, Python’s dataclasses module just got a game-changing upgrade! Now, you can effortlessly inherit dataclass features in both dataclass and non-dataclass child classes, automatically reducing boilerplate and preventing common coding errors. This update is a significant win for Python developers! |
Inheritance in Python allows one class to inherit attributes and methods from another, making your code more efficient and reusable. For example, a Dog class can inherit from an Animal class, gaining all the basic features like eat() and sleep().
But understanding how inheritance works can be tricky, especially when you need to manage complex relationships between classes.
This article will break it down simply, helping you use inheritance to write cleaner and more efficient code.
Improve your coding skills with upGrad’s online software engineering courses. Specialize in cybersecurity, full-stack development, and much more. Take the next step in your learning journey!
Popular Data Science Programs
Inheritance in Python is an object-oriented programming concept where a new class (called a derived or child class) inherits the properties and methods of an existing class (called a base or parent class).
This allows the child class to reuse code from the parent class, avoiding redundancy and promoting code reusability.
For example, you have a Base class of “Animal,” and a “Lion” is a Derived class. The inheritance will be Lion is an Animal.
So, the question is, what does the “Lion” class inherit from “Animal”?
A “Lion” class inherits
Note: You can replace the Derived Class objects with Base Class objects in an application known as the Liskov substitution principle. It indicates that if a computer program has object P as the subtype of Q, you can easily replace P with Q without altering the properties.
In 2025, professionals who can use advanced programming techniques to streamline business operations will be in high demand. If you're looking to develop skills in in-demand programming languages, here are some top-rated courses to help you get there:
The basic syntax to inherit from a parent class is:
class ChildClass(ParentClass):
# Additional methods and properties
Example:
class Animal:
def sound(self):
print("Sound made")
class Dog(Animal):
def bark(self):
print("Woof")
dog = Dog()
dog.sound() # Output: Sound made
dog.bark() # Output: Woof
Explanation: Dog inherits the sound() method from Animal, and we also define its own method bark().
Now that you have a basic understanding of what inheritance in Python is, let's look at the different types of inheritance. Each type serves a unique purpose and can be used depending on the complexity of your class hierarchy and the functionality you need to inherit.
Here’s a breakdown of the most common types of inheritance in Python:
Single inheritance is the simplest form of inheritance in Python, where a class (called the child or subclass) inherits from only one class (called the parent or base class). This allows the child class to use the attributes and methods defined in the parent class, making code reuse more efficient.
Single inheritance is useful when you want a class to inherit properties and methods from just one parent class. This makes it easier to manage and understand since there’s only one class providing the structure and behavior.
Let’s look at an example to better understand how single inheritance in Python works.
class Country:
def ShowCountry(self):
print(“This is Spain”);
class State(Country):
def ShowState(self):
print(“This is State”);
st =State();
st.ShowCountry();
st.ShowState();
Output:
This is Spain
This is State
Explanation:
Also Read: What are the Types of Inheritance in Java? Examples and Tips to Master Inheritance
When to Use:
Best Practices
If you're finding it tough to break down complex problems efficiently, check out upGrad’s Data Structures & Algorithms free course to strengthen your foundation and start solving challenges with ease. Start today!
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
Python is made of several objects, and with the multi-level inheritance, there are endless possibilities of reusing the class functionalities. Multi-level inheritance in Python gets documented each time a derived class inherits another derived class. There is no limit to the number of derived classes that can inherit the functionalities, and that is why multilevel inheritance helps to improve the reusability in Python.
Key Points:
Here is an example of multilevel inheritance
class Animal:
def speak(self):
print(“Animal Speaking”)
#The child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print(“dog barking”)
#The child class Dogchild inherits another child class Dog
class DogChild(Dog):
def eat(self):
print(“Eating bread…”)
d = DogChild()
d.bark()
d.speak()
d.eat()
Output:
dog barking
Animal Speaking
Eating bread...
Explanation:
When to Use:
Best Practices:
Also Read: Understanding the Differences Between Inheritance and Polymorphism in Java
Multiple inheritance in Python allows a class to inherit attributes and methods from more than one base class. This is particularly useful when a class needs to combine functionality from multiple sources.
Key Points:
Let’s look at an example for multiple inheritances.
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5
Explanation:
When to Use:
Best Practices:
Always ensure that the relationship between the classes makes sense and is easy to understand.
In hierarchical inheritance, multiple classes inherit from a single parent class. This allows for shared functionality to be used by all subclasses, which can then add their own unique methods.
Key Points:
Example Code:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
class Cat(Animal):
def meow(self):
print("Cat meows")
dog = Dog()
dog.speak() # Output: Animal speaks
dog.bark() # Output: Dog barks
cat = Cat()
cat.speak() # Output: Animal speaks
cat.meow() # Output: Cat meows
Output:
Animal speaks
Dog barks
Animal speaks
Cat meows
Explanation:
When to Use:
Best Practices:
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
For complex behaviors, prefer composition over inheritance. Remember, inheritance in Python allows you to reuse code, but it should be used in a way that keeps your codebase maintainable.
Let's put this into practice with a real-life application example, a Vehicle Management System.
You’ll learn how to organize your code and make it more maintainable by applying all four types of inheritance in Python: single, multi-level, multiple, and hierarchical. This example will demonstrate how inheritance in Python can simplify complex systems and bring real value to your projects.
In this example, we will model vehicles, such as cars, trucks, and bikes, using different types of inheritance.
# Single Inheritance
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"Vehicle Brand: {self.brand}")
print(f"Vehicle Model: {self.model}")
class Car(Vehicle): # Single Inheritance
def __init__(self, brand, model, seats):
super().__init__(brand, model) # Calling the parent class constructor
self.seats = seats
def car_info(self):
self.display_info()
print(f"Seats: {self.seats}")
# Multi-Level Inheritance
class ElectricVehicle(Vehicle): # Multi-Level Inheritance
def __init__(self, brand, model, battery_capacity):
super().__init__(brand, model)
self.battery_capacity = battery_capacity
def battery_info(self):
print(f"Battery Capacity: {self.battery_capacity} kWh")
class ElectricCar(ElectricVehicle): # Multi-Level Inheritance
def __init__(self, brand, model, seats, battery_capacity):
super().__init__(brand, model, battery_capacity)
self.seats = seats
def electric_car_info(self):
self.car_info()
self.battery_info()
# Multiple Inheritance
class Truck:
def __init__(self, cargo_capacity):
self.cargo_capacity = cargo_capacity
def truck_info(self):
print(f"Cargo Capacity: {self.cargo_capacity} tons")
class DeliveryVan(Car, Truck): # Multiple Inheritance
def __init__(self, brand, model, seats, cargo_capacity):
Car.__init__(self, brand, model, seats)
Truck.__init__(self, cargo_capacity)
def delivery_van_info(self):
self.car_info()
self.truck_info()
# Hierarchical Inheritance
class Bike(Vehicle): # Hierarchical Inheritance
def __init__(self, brand, model, type_of_bike):
super().__init__(brand, model)
self.type_of_bike = type_of_bike
def bike_info(self):
self.display_info()
print(f"Bike Type: {self.type_of_bike}")
# Creating objects and displaying information
# Single Inheritance (Car)
car = Car("Toyota", "Corolla", 5)
car.car_info()
print("\n--- Multi-Level Inheritance ---")
# Multi-Level Inheritance (ElectricCar)
electric_car = ElectricCar("Tesla", "Model 3", 5, 75)
electric_car.electric_car_info()
print("\n--- Multiple Inheritance ---")
# Multiple Inheritance (DeliveryVan)
delivery_van = DeliveryVan("Ford", "Transit", 2, 10)
delivery_van.delivery_van_info()
print("\n--- Hierarchical Inheritance ---")
# Hierarchical Inheritance (Bike)
bike = Bike("Yamaha", "FZ", "Sport")
bike.bike_info()
Output:
Vehicle Brand: Toyota
Vehicle Model: Corolla
Seats: 5
--- Multi-Level Inheritance ---
Vehicle Brand: Tesla
Vehicle Model: Model 3
Seats: 5
Battery Capacity: 75 kWh
--- Multiple Inheritance ---
Vehicle Brand: Ford
Vehicle Model: Transit
Seats: 2
Cargo Capacity: 10 tons
--- Hierarchical Inheritance ---
Vehicle Brand: Yamaha
Vehicle Model: FZ
Bike Type: Sport
Explanation:
This chain adds additional features step by step, first Vehicle, then ElectricVehicle (for battery capacity), and finally ElectricCar (combining both vehicle and battery info).
Start by implementing simple examples, then gradually build more complex systems. Focus on understanding when to use each type of inheritance and how to structure your classes for better readability and maintainability.
Next, let’s look at how upGrad can assist in your learning journey.
Inheritance in Python offers powerful ways to organize and reuse code, allowing you to create more efficient and maintainable applications. However, mastering inheritance in Python can be challenging, especially when dealing with complex hierarchies or multiple inheritance scenarios.
To excel, focus on practicing with simple examples, then gradually build more intricate systems to gain confidence. For further growth in your Python journey, upGrad’s courses in Python and OOPs can deepen your understanding and help you tackle more advanced challenges.
In addition to the courses mentioned above, here are some more free courses that can help you elevate your skills:
Not sure which direction to take in your career or feeling uncertain about your next step? You can get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today!
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals
References:
https://discuss.python.org/t/dataclasses-and-non-dataclasses-inheritance/88840
https://stackify.com/solid-design-liskov-substitution-principle/
Inheritance refers to the process of passing on the properties of a parent class to a child class. It is an object-oriented programming(OOP) concept and is significant in Python. It is because inheritance provides code reusability, which means that instead of writing the same code over and again, we can inherit the attributes we require in a child class. It is also easy to understand and implement since it depicts a real-world relationship between the parent and child classes. It has a transitive nature. Because all child classes inherit properties from their parents, any subclasses will likewise use the parent class's functionalities.
Python allows all forms of inheritance, including multiple inheritances, unlike other object-oriented programming languages. It is possible to construct new classes from pre-existing ones using the inheritance concept. This facilitates the reuse of code. The methods specified in the parent class are also used in the child class. While C++ also enables this sort of inheritance, it lacks Python's advanced and well-designed methodology. Python even offers Hybrid inheritance, which allows us to implement many types of inheritance in a single piece of code. Because code reusability is a strength of inheritance, it is helpful in a wide range of applications when working with Python.
Yes, you can override methods in a child class in Python. When you inherit from a parent class, the child class can redefine any inherited method to provide specific functionality. This allows you to customize behavior while still retaining access to the parent class’s methods.
In single inheritance, a child class inherits from one parent class, while in multi-level inheritance, a class inherits from a derived class, forming a chain. Multi-level inheritance allows for greater flexibility by building upon inherited features, but it can lead to more complex code.
Multiple inheritance allows a class to inherit from more than one parent class. It’s useful when a class needs to combine functionality from different sources. However, inheritance in Python can become tricky if the parent classes have conflicting methods, so it’s important to ensure that the classes complement each other.
To avoid conflicts when using multiple inheritance, make sure that the parent classes do not define methods with the same name unless they are intentionally meant to be overridden. Python's Method Resolution Order (MRO) helps resolve conflicts, but it’s important to design classes with clear responsibilities to minimize issues with inheritance in Python.
Abstract classes in Python provide a template for other classes, defining methods that must be implemented by any subclass. These classes cannot be instantiated directly, but they play a crucial role in inheritance in Python by enforcing a consistent interface across subclasses, ensuring that they implement certain methods.
While inheritance in Python is designed to reduce redundancy, it can sometimes lead to code bloat if it’s overused or used incorrectly. For example, deeply nested class hierarchies can make code harder to read and maintain. It’s important to use inheritance where it logically adds value and simplifies your design.
Inheritance in Python allows you to create flexible systems by enabling classes to inherit functionality and modify or extend it. This makes it easier to build systems that can adapt to change, as new features can be added through subclasses without altering the existing code structure.
Yes, inheritance in Python supports dynamic behavior changes through method overriding. A subclass can change or extend the behavior of a method defined in the parent class. This allows objects of different classes in the same hierarchy to exhibit unique behavior while sharing common functionality.
Inheritance in Python helps reduce technical debt by promoting code reuse and reducing redundancy. By creating reusable base classes, you avoid rewriting similar code in multiple places, making it easier to maintain and extend. It also helps ensure that updates to shared functionality are made in one central location.
408 articles published
Rohan Vats is a Senior Engineering Manager with over a decade of experience in building scalable frontend architectures and leading high-performing engineering teams. Holding a B.Tech in Computer Scie...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources