📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

Inheritance in Python: Complete Guide with Examples

Quick answer: Learn Python inheritance including parent and child classes, method overriding, the super() function, and multiple inheritance, with examples.

What is Inheritance?

Inheritance lets a class, called the child or subclass, reuse and extend the attributes and methods of another class, called the parent or superclass, avoiding repeated code across related classes.

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

    def speak(self):
        return "Some generic sound"

class Dog(Animal):
    def speak(self):
        return "Woof"

class Cat(Animal):
    def speak(self):
        return "Meow"

d = Dog("Rex")
c = Cat("Whiskers")
print(d.name, d.speak())   # Rex Woof
print(c.name, c.speak())   # Whiskers Meow

Both Dog and Cat automatically inherit the __init__ method and the name attribute from Animal, without needing to redefine them.

Method Overriding

A child class can redefine a method it inherits, replacing the parent's version with its own, as seen with speak() above. This is called method overriding.

Using super()

super() lets a child class call a method from its parent class, commonly used inside an overridden __init__ to still run the parent's setup logic.

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

class Manager(Employee):
    def __init__(self, name, salary, team_size):
        super().__init__(name, salary)   # runs Employee's __init__ first
        self.team_size = team_size

m = Manager("Rohan", 80000, 5)
print(m.name, m.salary, m.team_size)

Checking Inheritance

print(isinstance(d, Animal))    # True, Dog is a subclass of Animal
print(issubclass(Dog, Animal))  # True

Multiple Inheritance

Python allows a class to inherit from more than one parent class, though it should be used carefully to avoid confusing method resolution when parents define the same method.

class Swimmer:
    def swim(self):
        return "Swimming"

class Runner:
    def run(self):
        return "Running"

class Triathlete(Swimmer, Runner):
    pass

t = Triathlete()
print(t.swim(), t.run())

Common Interview Questions

What is the difference between inheritance and method overriding?

Inheritance is the mechanism of a class acquiring attributes and methods from another class. Method overriding is when a child class provides its own specific implementation of a method it inherited.

Why would you use super() instead of skipping the parent's __init__ entirely?

Skipping it means you would need to manually reinitialise every attribute the parent normally sets up, duplicating logic and risking it going out of sync if the parent class changes later.

FAQ

Frequently Asked Questions

What is inheritance in Python used for?

It lets a class reuse and extend the attributes and methods of another class, avoiding repeated code across related classes.

What is method overriding?

When a child class defines its own version of a method it inherited from a parent class, replacing the parent's behaviour for that class.

What does super() do in Python?

It lets a child class call a method, most commonly __init__, from its parent class, so the parent's setup logic still runs.

Does Python support multiple inheritance?

Yes, a class can inherit from more than one parent class, though it should be used carefully when parent classes define overlapping methods.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles