View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Inheritance in Python: Types, Best Practices & Examples

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! 

What is an inheritance in Python?

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

  • Interface
  • Execution 

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:

1. Single Inheritance

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:

  • st.ShowCountry(): The State class inherits from Country, so when the ShowCountry() method is called on the st object, it calls the method defined in the Country class. The output will be "This is Spain".
  • st.ShowState(): This method is specific to the State class, and when it is called, the output is "This is State".

Also Read: What are the Types of Inheritance in Java? Examples and Tips to Master Inheritance

When to Use:

  • When you need a straightforward relationship between a parent class and a child class.
  • Ideal for simple hierarchies where one class extends the functionality of another.
  • Best when the child class only needs to inherit from a single source of common functionality.

Best Practices

  • Keep it Simple: Don’t overcomplicate things by creating unnecessary parent-child relationships. Use inheritance in Python only when it makes sense.
  • Use Descriptive Names: Name your parent and child classes in a way that reflects their relationship clearly. For example, Animal and Dog are good class names because it’s clear that a Dog is a type of Animal.
  • Avoid Overuse of Inheritance: While inheritance is a powerful feature, avoid deep inheritance chains. Overusing inheritance in Python can lead to complexity and confusion, especially when the class hierarchy becomes too long.

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!

2. Multi-Level inheritance

background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

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:

  • You can have multiple levels of inheritance.
  • Each class in the chain adds its own specific functionality, building on top of the inherited behavior.

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:

  • d.bark(): The DogChild object d is created from the DogChild class, which inherits from the Dog class. The Dog class defines the bark() method, so calling d.bark() prints "dog barking".
  • d.speak(): The DogChild class inherits from the Dog class, which in turn inherits from the Animal class. Therefore, the DogChild object d can access the speak() method from the Animal class, printing "Animal Speaking".
  • d.eat(): The DogChild class has its own method eat() that prints "Eating bread...". Since the DogChild class defines this method, calling d.eat() prints "Eating bread...".

When to Use:

  • When you need to build a chain of classes, where each subclass adds more specific functionality.
  • Useful when extending features from a parent class to multiple levels of subclasses.
  • Appropriate for modeling hierarchies that evolve with more specialized features as the chain progresses.

Best Practices: 

  • Keep Class Hierarchies Shallow: Avoid long and deep inheritance chains to prevent complexity and confusion.
  • Use super() Carefully: Ensure proper method resolution order (MRO) by using super() to call parent class methods.
  • Prefer Composition Over Inheritance: Use composition for shared behavior instead of deep inheritance chains when possible.
  • Ensure Clear Hierarchy: Maintain a logical and understandable class hierarchy that reflects real-world relationships.

Also Read: Understanding the Differences Between Inheritance and Polymorphism in Java

3. Multiple Inheritance

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:

  • A child class can inherit from multiple classes, gaining access to all of their methods and properties.
  • Potential for ambiguity if multiple base classes have methods with the same name, but Python’s Method Resolution Order (MRO) helps resolve this.

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:

  • d.Summation(10, 20): The Derived class inherits from Calculation1, so it can call the Summation method, which adds the two numbers 10 and 20. The output will be 30.
  • d.Multiplication(10, 20): The Derived class also inherits from Calculation2, so it can call the Multiplication method, which multiplies the two numbers 10 and 20. The output will be 200.
  • d.Divide(10, 20): The Divide method is specific to the Derived class and divides the two numbers 10 and 20. The output will be 0.5.

When to Use:

  • When a class needs to inherit behavior from more than one parent class.
  • Ideal when you want to combine functionality from multiple sources without duplicating code.
  • Best used when the classes have distinct but complementary functionality that doesn't lead to conflicts.

Best Practices:

  • Keep Class Hierarchies Shallow: Avoid deep and complicated inheritance structures. In this case, the inheritance in Python is simple and clear.
  • Prefer Composition Over Inheritance: If Summation, Multiplication, and Divide are distinct operations, consider using composition instead of inheritance.  
  • Ensure Clear Hierarchy: The class hierarchy here is straightforward and logical, with Derived clearly inheriting from Calculation1 and Calculation2. 

Always ensure that the relationship between the classes makes sense and is easy to understand.

4. Hierarchical Inheritance

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:

  • A parent class is shared among multiple child classes.
  • Each child class can inherit the functionality from the parent and extend it as needed.

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:

  • dog.speak(): The Dog class inherits from the Animal class. When the speak() method is called on the dog object, it uses the inherited speak() method from Animal, which outputs "Animal speaks".
  • dog.bark(): The Dog class defines its own method bark(), which is specific to dogs. Calling dog.bark() outputs "Dog barks".
  • cat.speak(): The Cat class also inherits from Animal. When the speak() method is called on the cat object, it outputs "Animal speaks" because Cat inherits the speak() method from Animal.
  • cat.meow(): The Cat class has its own method meow(), which is specific to cats. Calling cat.meow() outputs "Cat meows".

When to Use:

  • When multiple classes need to share common functionality from a single parent class.
  • Ideal for situations where several subclasses share a base set of behaviors or attributes.
  • Best for creating a consistent interface across different child classes that extend from the same parent class.

Best Practices:

  • Favor Composition Over Inheritance in Python for Complex Behaviors: Use composition for specialized behavior (e.g., flying, swimming) instead of extending a class.
  • Avoid Overusing Inheritance for Small Behaviors: Use direct method calls for trivial behaviors instead of adding unnecessary inheritance.
  • Use Inheritance in Python to Define Common Interface: Define shared methods in the parent class and leave behavior-specific methods to subclasses.
  • Ensure Methods in Child Classes Are Specific: Ensure each subclass has its own unique methods to maintain code modularity and clarity.

Struggling to integrate complex functionalities into your web applications? Check out the AI-Powered Full Stack Development Course by IIITB will help you master innovative tools to streamline your development process. Start today!

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.

Vehicle Management System: Python Inheritance Example

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:

  • Single Inheritance (Vehicle and Car): The Car class inherits from the Vehicle class, which provides common functionality (like display_info()), and adds its own feature (seats).
  • Multi-Level Inheritance (Vehicle, ElectricVehicle, and ElectricCar): The ElectricCar class inherits from ElectricVehicle, which in turn inherits from Vehicle. 

This chain adds additional features step by step, first Vehicle, then ElectricVehicle (for battery capacity), and finally ElectricCar (combining both vehicle and battery info).

  • Multiple Inheritance (Car, Truck, and DeliveryVan): The DeliveryVan class inherits from both Car and Truck, combining their features. It can now display both car-specific features (like seats) and truck-specific features (like cargo_capacity).
  • Hierarchical Inheritance (Vehicle, Bike): The Bike class inherits from the Vehicle class, meaning all Bike objects get the shared functionality of Vehicle (like display_info()), but also add specific attributes (like type_of_bike).

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.

If you're eager to take your understanding of inheritance in Python and apply it to emerging fields like cryptography and data security, enroll in upGrad’s DBA in Emerging Technologies with Concentration in Generative AI. Master the techniques behind intelligent, data-driven applications. Start today!
 

Next, let’s look at how upGrad can assist in your learning journey.

Learn Inheritance in Python with upGrad!

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/

Frequently Asked Questions (FAQs)

1. Why is inheritance significant in Python?

2. In Python, which types of inheritance are not supported?

3. Can I override methods in a child class in Python?

4. What is the difference between single and multi-level inheritance in Python?

5. How can multiple inheritance be used effectively in Python?

6. How do I avoid conflicts when using multiple inheritance in Python?

7. How do abstract classes relate to inheritance in Python?

8. Can inheritance in Python lead to code bloat?

9. How can inheritance be used to implement flexible systems in Python?

10. Does inheritance in Python support dynamic behavior changes?

11. What role does inheritance play in minimizing technical debt in Python projects?

Rohan Vats

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

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

17 Months

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months