Python 3D Academyবাংলা

Overriding methods and super()

🌍 In real life. Every phone can make a sound when a call comes in. A school phone just rings; a smartphone plays your favourite song instead. The smartphone still is a phone, but it does that one thing its own way.

In Python, a child class can override a method: it writes a method with the same name as the parent's, and its own version wins. That's the lookup tower again: the child's floor is searched before the parent's.

The idea in one sentence: a child overrides a parent's method by writing one with the same name, and super().method() still lets it run the parent's version too.

Its words:

  • override: replace a parent's method in the child
  • super(): "my parent class", used to call the parent's version
  • extend: run the parent's version and then add to it, like super().__init__(name) followed by the child's own setup
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return "..."

    def describe(self):
        return self.name + " says " + self.speak()

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

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

print(Dog("Rex").describe())
print(Cat("Tom").describe())
print(Animal("Blob").describe())
describe lives in Animal, but self.speak() finds each object's own version.

Sometimes the child wants to add to the parent's work instead of replacing it. super() means "my parent class":

  • super().__init__(name) runs the parent's __init__ first
  • then the child sets up its own extras

Think of a new student joining a sports club: they get the normal school record (the parent's part) plus a club badge (the child's extra).

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

    def greet(self):
        return "Hi, I'm " + self.name

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

    def greet(self):
        return super().greet() + " from " + self.school

s = Student("Ana", "Green School")
print(s.name, s.school)
print(s.greet())

Why super() instead of copying? If the child copied the parent's setup code, a later change to the parent would have to be made twice, and one copy would be forgotten. super().__init__(...) always runs the parent's current version.

Mistakes and edge cases.

  • Forgetting super().init. If the child writes its own __init__ and never calls the parent's, the parent's attributes are never set: AttributeError: 'B' object has no attribute 'n'.
  • A different name by accident. def speek(self): doesn't override speak; the parent's version still runs.
  • Calling super without brackets. It's super().speak(), with brackets after super.

Quiz

  1. What does this print?

    class Bird:
        def move(self):
            return "flies"
    
    class Penguin(Bird):
        def move(self):
            return "swims"
    
    print(Penguin().move(), Bird().move())
  2. What does this print?

    class Shape:
        def name(self):
            return "shape"
    
    class Square(Shape):
        def name(self):
            return "square, a kind of " + super().name()
    
    print(Square().name())
  3. What does super().__init__(name) do inside a child's __init__?

    • Runs the parent's __init__, so the parent's attributes get set
    • Makes a new parent object
    • Deletes the child's __init__
  4. What does this print?

    class Animal:
        def __init__(self, name):
            self.name = name
    
    class Dog(Animal):
        def __init__(self, name, trick):
            super().__init__(name)
            self.trick = trick
    
    d = Dog("Rex", "roll over")
    print(d.name + " can " + d.trick)
  5. Which line has a mistake?

    class Animal:
        def __init__(self, name):
            self.name = name
    
    class Dog(Animal):
        def __init__(self, name, trick):
            self.trick = trick
    
    d = Dog("Rex", "sit")
    print(d.name)

Coding tasks

  1. Animal is written for you. Write Cat, a kind of Animal, whose speak() returns "Meow" instead.

  2. Rectangle is written for you. Write Square(side), a kind of Rectangle, whose __init__ uses super().__init__ so that width and height are both side.

  3. Dog is written for you. Write Puppy, a kind of Dog, whose speak() gives the Dog's sound with an extra "!" on the end, using super().