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 humstart 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 5super().__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.