Inheritance in Python with Examples

Inheritance in Python lets a child class automatically acquire the attributes and methods of a parent class, eliminating duplicate code. Python supports single, multilevel, and multiple inheritance. The class sharing its properties is the parent class; the class receiving them is the child class. Use the super() function to call parent methods from inside a child class.

Key Takeaways

  • Inheritance lets a child class reuse code from a parent class without rewriting it.
  • Python supports single, multilevel, and multiple inheritance natively.
  • The super() function calls a parent class method from inside the child class.
  • Method Resolution Order (MRO) controls the sequence Python uses to search for a method across multiple parent classes.
  • Strong OOP skills, including inheritance, are expected in Python-related job interviews at Indian IT firms like Infosys, TCS, and Wipro.

If you are just getting started with the language, read how to write and run a Python program before going further.

Inheritance in Python

When you write real applications, repeating the same code in every class is a maintenance nightmare. Inheritance solves that by letting a child class get all the properties and behaviours of a parent class automatically. You only write shared logic once, and every derived class benefits from it instantly.

The Stack Overflow Developer Survey 2024 found Python to be the most used programming language among professional developers for the third consecutive year. A huge reason for that popularity is how clean Python’s OOP syntax is compared to Java or C++. According to the JetBrains State of Developer Ecosystem 2023, 57% of Python developers use object-oriented programming as their primary coding style, making inheritance one of the most practised Python concepts globally. The TIOBE Index for April 2025 ranks Python at number one globally, and its cooperative super() pattern is one reason experienced developers praise its OOP model.

Basic Syntax

To create a child class in Python, you pass the parent class name inside parentheses when defining the child:

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} makes a sound."

class Dog(Animal):
    def speak(self):
        return f"{self.name} barks."

d = Dog("Bruno")
print(d.speak())  # Output: Bruno barks.

In this inheritance in Python with example, Dog inherits the __init__ method from Animal but overrides the speak method with its own version. That is method overriding in Python, one of the most useful features inheritance gives you.

Types of Inheritance in Python

Python supports five types of inheritance: single, multilevel, multiple, hierarchical, and hybrid. The table below compares Python and Java across the most commonly tested types, which appear regularly in BCA, BTech, and MCA semester exams at universities like Anna University, VTU, and AKTU.

Feature Python Java
Single inheritance Yes Yes
Multiple inheritance (classes) Yes No (interfaces only)
Multilevel inheritance Yes Yes
Hierarchical inheritance Yes Yes
Syntax keyword Parentheses: class Child(Parent) extends keyword
MRO algorithm C3 linearisation Fixed left-to-right (interfaces)
super() usage super().method() super.method()

Single and Multilevel Inheritance

Single inheritance is the simplest form: one child class inherits from exactly one parent class. The Dog example above is a classic case. Most beginner-level OOP problems, including those asked in BCA and BTech semester exams, use this form.

Single Inheritance Example in Python

Here is an inheritance in Python with example using a university management scenario that students across India will recognise:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def info(self):
        return f"Name: {self.name}, Age: {self.age}"

class Student(Person):
    def __init__(self, name, age, roll_no):
        super().__init__(name, age)
        self.roll_no = roll_no

    def info(self):
        return f"{super().info()}, Roll No: {self.roll_no}"

s = Student("Priya", 20, "CS2024")
print(s.info())
# Output: Name: Priya, Age: 20, Roll No: CS2024

Notice how super().__init__(name, age) calls the parent constructor so you do not duplicate the attribute assignment. This is exactly the pattern you will see in production Django models and Flask apps. Python developers at Indian product companies like Freshworks and Zoho use this pattern daily in their backend services.

Multilevel Inheritance in Python

Multilevel inheritance chains classes across more than two levels. Class B inherits from A, and class C inherits from B. Python traces the chain all the way up when it looks for a method.

class Vehicle:
    def start(self):
        return "Vehicle started"

class Car(Vehicle):
    def fuel_type(self):
        return "Petrol"

class ElectricCar(Car):
    def fuel_type(self):
        return "Electric"

ec = ElectricCar()
print(ec.start())      # Inherited from Vehicle
print(ec.fuel_type())  # Overridden in ElectricCar

A common mistake here is assuming the child class cannot access grandparent methods. It absolutely can, because Python walks the entire inheritance chain. The order it uses is determined by MRO, which we cover next.

Multiple Inheritance and MRO

Python allows a single class to inherit from two or more parent classes simultaneously. That is multiple inheritance, and it is one area where Python genuinely stands apart from Java. According to the official Python 3 documentation, Python uses the C3 linearisation algorithm to determine the order in which base classes are searched.

Multiple Inheritance Example in Python

class Father:
    def skills(self):
        return "Coding"

class Mother:
    def skills(self):
        return "Design"

class Child(Father, Mother):
    pass

c = Child()
print(c.skills())  # Output: Coding

Python picks Father.skills() because Father appears first in Child(Father, Mother). Change the order to Child(Mother, Father) and you will get “Design” instead. This is not a bug; it is MRO working exactly as designed.

What is MRO in Python?

Method Resolution Order (MRO) is the sequence Python follows to look up a method or attribute across a class hierarchy. Python calculates MRO using the C3 linearisation algorithm, which guarantees a consistent, predictable order. You can inspect it with the built-in __mro__ attribute or the mro() method:

print(Child.__mro__)
# (<class '__main__.Child'>, <class '__main__.Father'>,
#  <class '__main__.Mother'>, <class 'object'>)

Every class in Python ultimately inherits from object, which is why object always appears at the end of the MRO chain. This matters a lot when you are building frameworks or working with libraries like Django ORM, where deep class hierarchies are common.

Understanding OOP well opens doors beyond pure programming. If you are curious how these concepts apply in data science work, the Pandas and NumPy explained guide shows you how Python’s class system underpins both libraries.

Using super() in Python Inheritance

The super() function gives you a proxy object that lets you call methods from the parent class without naming it explicitly. This keeps your code flexible: if you rename the parent class later, you do not have to hunt through every child class to update the reference.

Python Inheritance Constructor Example with super()

class Animal:
    def __init__(self, name):
        self.name = name
        print(f"Animal __init__ called for {self.name}")

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed
        print(f"Dog __init__ called, breed: {self.breed}")

d = Dog("Bruno", "Labrador")
# Animal __init__ called for Bruno
# Dog __init__ called, breed: Labrador

Always call super().__init__() in the child’s constructor when the parent class sets up important attributes. Forgetting this is one of the most common bugs in beginner Python projects, and it produces confusing AttributeError messages that are hard to trace. This is a frequent interview question at Infosys, TCS, and Wipro campus drives across India.

These skills translate directly into real-world work. Python’s OOP patterns power everything from Python for automation, web scraping and APIs to machine learning pipelines. If you are thinking about an AI career, see how these fundamentals fit into the bigger picture in our guide on how to become an AI engineer.

Choosing the right tools alongside strong language knowledge matters too. The software development tools to learn first guide pairs well with everything you are learning here. And if you are a student planning your academic path, the BSc Computer Science subjects and scope article explains where OOP sits in the broader curriculum.

Frequently Asked Questions

What is inheritance in Python?

Inheritance in Python is an OOP mechanism where a child class automatically acquires the attributes and methods of a parent class. It promotes code reuse and lets you build specialised classes from general ones. Python supports single, multilevel, and multiple inheritance, all with straightforward syntax.

Can you give a Python inheritance example?

Yes. Define a parent class Animal with a speak() method, then create a child class Dog(Animal) that overrides speak() to return “barks”. The child inherits everything else from Animal without any extra code. This is a standard single inheritance in Python with example used in tutorials everywhere.

What are the types of inheritance in Python?

Python supports five types of inheritance: single (one parent, one child), multilevel (chained classes), multiple (one child, two or more parents), hierarchical (one parent, multiple children), and hybrid (a combination of the above). All five are supported natively without interfaces or workarounds.

Does Python support multiple inheritance?

Yes, Python supports multiple inheritance natively. A class can inherit from two or more parent classes by listing them in parentheses: class Child(Parent1, Parent2). Java does not allow this with classes; it uses interfaces instead. Python manages the complexity through its MRO algorithm.

What is MRO in Python?

MRO stands for Method Resolution Order. It is the sequence Python uses to search for a method or attribute through a class hierarchy, especially when multiple inheritance is involved. Python computes it using the C3 linearisation algorithm. You can inspect it with ClassName.__mro__ or ClassName.mro().

How does super() work in Python?

The super() function returns a proxy object that delegates method calls to the parent class following the MRO. You use it most often in __init__ to call the parent constructor without hardcoding the parent class name. In multiple inheritance, super() ensures every class in the chain is initialised correctly when used cooperatively.

What to do this week: you have seen how inheritance in Python with example code works, from single inheritance right through to MRO and super(). The concepts are straightforward once you write them yourself rather than just reading them. Here are three concrete next steps to lock in what you have learned:

  1. Write a three-level multilevel inheritance chain using a domain you know, like a university system with Person, Employee, and Professor classes, and print the MRO for each.
  2. Build a small multiple inheritance in Python example where two parent classes share a method name, then experiment with changing the order of parents and observing the output.
  3. Apply these skills in a real project context. The Python for automation, web scraping and APIs guide shows you exactly where OOP patterns appear in production-level Python code.

Strong OOP fundamentals are the foundation for ethical hacking (think writing reusable exploit modules), secure coding, AI model development, and blockchain applications. If you want to turn these programming skills into a career, explore the CEH v13 AI online course at 3.0 University, or browse the full range of 3.0 University online certification courses in Cybersecurity, AI, and Blockchain to find the path that fits your goals.

Last updated: May 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles