Stop 07 · Object-Oriented ProgrammingTopics · 5/10

Inheritance & super()

Part of the Python Roadmap

In short

When two classes share most of their code, copying it is a recipe for bugs. Inheritance lets a child class reuse a parent's code and change only what's different.

A child class inherits attributes and methods from a parent class and can override or extend them. super() calls the parent's version, most often inside __init__.

The jaadu receives an old handwritten recipe book from a grandmother figure and adds a small new ingredient to a pot.

Think of it like this

A family recipe passed down

Your grandmother's biryani recipe gets handed to your mother, who keeps it all but adds a pinch of kewra. Your mother doesn't rewrite the recipe from scratch; she uses Nani's steps and adds her own touch. super() is your mother saying 'first do it Nani's way, then add mine'.

Watch it happen

Employee family tree

Employee
Developer
Manager
Intern

1/4Employee has name, salary and a describe() method.

The big ideas

What you need to know

class Child(Parent)

The child gets every attribute and method of the parent automatically.

Override

Define a method with the same name in the child to change its behaviour for that child only.

super() reaches up

super().__init__(...) runs the parent's setup so you don't repeat it. Use it in any method to extend rather than replace.

Is-a, not has-a

Inherit only when the child truly is a kind of parent. A Car has an Engine, it isn't one, so use composition there.

See it in code

Walk through the code

python
1class Employee:2    def __init__(self, name: str, salary: float):3        self.name = name4        self.salary = salary5 6    def describe(self) -> str:7        return f"{self.name} earns Rs {self.salary:,.0f}"8 9    def annual_bonus(self) -> float:10        return self.salary * 0.1011 12class Manager(Employee):13    def __init__(self, name: str, salary: float, team_size: int):14        super().__init__(name, salary)15        self.team_size = team_size16 17    def describe(self) -> str:18        base = super().describe()19        return f"{base} and leads {self.team_size} people"20 21    def annual_bonus(self) -> float:22        return super().annual_bonus() + 1000 * self.team_size23 24m = Manager("Priya", 90000, 6)25print(m.describe())        # Priya earns Rs 90,000 and leads 6 people26print(m.annual_bonus())    # 15000.027print(isinstance(m, Employee))   # True

1/6The parent sets up shared attributes.

A Manager reuses Employee's setup and methods, then extends them with team details.

Save yourself the pain

Common mistakes

Oops

Writing a child __init__ and forgetting to call super().__init__().

Do this instead

The parent's attributes never get set, leading to AttributeError later. Call super().__init__(...) first.

Oops

Building deep chains like A -> B -> C -> D -> E just to share one method.

Do this instead

Keep hierarchies shallow. Prefer composition or a helper function when classes aren't truly 'a kind of' each other.

Your mission

Vehicles of India

~25 min

Quick check

What does super().__init__(name, salary) do inside Manager.__init__?

Go deeper

  1. 1Create a SavingsAccount that inherits from BankAccount and adds interest.
  2. 2Call super().__init__() so the parent sets up its attributes.
  3. 3Keep inheritance shallow and only use it for true 'is a' relationships.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge