Legends ofPythos
Claim your name
Objects and Classes

super() and overriding

Lesson 9 of 13

Watch the lesson5:34 · with Torsten
Inheritance lets a child class reuse its parent’s code. But real programs often need the child to behave differently in specific spots while keeping everything else intact. That is where overriding and super() come together.

The mental model

Overriding a method

class Vehicle:
    def start(self):
        return "Engine roars"

class ElectricCar(Vehicle):
    def start(self):
        return "Silent hum"

car = ElectricCar()
print(car.start())   # Silent hum
ElectricCar replaces the parent’s start entirely.
When you call car.start(), Python looks at the object’s actual class first. Because ElectricCar defines its own start, that version wins and Vehicle.start is never touched for this instance. (Vehicle.start means the start method inside class Vehicle:. Written this way, self is often left out of the brackets: Employee.__init__(name) means def __init__(self, name): inside class Employee:.)

Using super() to extend, not replace

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

class Manager(Employee):
    def __init__(self, name, team_size):
        super().__init__(name)
        self.team_size = team_size

ana = Manager('Ana', 5)
print(ana.name, ana.team_size)   # Ana 5
super().__init__() runs the parent’s setup before adding team_size.
Without that line, a Manager object would have no .name, and any method expecting it would crash. The call hands control to the parent constructor with exactly the arguments it needs, then returns so you can finish your own setup.

Calling the parent’s behaviour inside an override

class Shape:
    def describe(self):
        return "generic shape"

class Square(Shape):
    def describe(self):
        base = super().describe()
        return f"{base} with four equal sides"

print(Square().describe())
super().describe() fetches the parent’s string, then you build on it.
The example shows a pattern: grab what the parent already gives you, add to it or change it, and return the richer result. Here, Square().describe() gives generic shape with four equal sides. You write the parent's code once, instead of copying it into every child class.

A common mistake

You’ll see this pattern a lot. A game might have an Enemy class whose __init__ sets up health, and a Dragon subclass whose own __init__ calls super().__init__() first, then adds fire breath. Master overriding and super() now, and building on other people’s classes will feel natural.

Your turn

0 of 3 solved

Exercise 1

+40 XP
Create two classes, Shape and Square. Shape.__init__(name) stores name as self.name, and Shape has an area() method that returns 0. Then create Square(Shape) with its own __init__(side), which calls super().__init__('square') first, then sets self.side to side. Finally, override area() in Square so it returns the side squared: self.side * self.side.

(Shape.__init__(name) means the __init__ inside class Shape:, written def __init__(self, name):; every method still takes self first.)
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+40 XP
Build Vehicle and ElectricCar. Vehicle.__init__(make) stores make as self.make. Give Vehicle a start() method that returns the text 'Engine roaring' (not the page's 'Engine roars'). ElectricCar inherits from Vehicle. Its __init__(make, battery_kw) calls super().__init__(make), then sets self.battery_kw to battery_kw. Override its start() so it calls super().start() and adds ', silent' to the end.

(Vehicle.__init__(make) means the __init__ inside class Vehicle:, written def __init__(self, make):; every method still takes self first.)
Write your Python here.

Run your code to check it against the tests.

Exercise 3

+40 XP
Create Employee and Manager. Employee.__init__(name, salary) stores both as self.name and self.salary. Give Employee a pay() method that returns the text 'pays <salary>', for example 'pays 90' for a salary of 90 (use an f-string). Manager inherits from Employee. Its __init__(name, salary, team_size) calls super().__init__(name, salary), then sets self.team_size to team_size. Override its pay() so it calls super().pay() and adds ' plus bonus' to the end, for example 'pays 90 plus bonus'.

(Employee.__init__(name, salary) means the __init__ inside class Employee:, written def __init__(self, name, salary):; every method still takes self first.)
Write your Python here.

Run your code to check it against the tests.