Inheritance in Python: Types, Best Practices & Examples
By Rohan Vats
Updated on Jun 30, 2025 | 12 min read | 19.76K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Jun 30, 2025 | 12 min read | 19.76K+ 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!
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!
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:
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/
408 articles published
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources