L2 · Working engineerObject-oriented Python~4 min · 3 tests#73

Method Overriding and super()

Override a parent method in a Python subclass and still call the parent version with super(). Polymorphism explained with an Animal/Dog example.

The problem

  • Animal.speak() returns "Some sound".
  • Dog(Animal) overrides speak() to return "Bark".
  • Dog.both() returns a tuple (parent's speak, own speak) using super() — ("Some sound", "Bark").

Examples

  1. Example 1

    Input

    (Animal().speak(), Dog().speak())

    Expected output

    ('Some sound', 'Bark')
  2. Example 2

    Input

    Dog().both()

    Expected output

    ('Some sound', 'Bark')

+ 1 hidden test on Submit — polymorphism.

Edge cases to ask about

  • Calling the parent version
  • Mixed collections

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · Dog
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Override + super()O(1)O(1)super() delegates to the next class in the MRO.
    Walkthrough of the optimal approach (try it yourself first)

    Defining speak in Dog overrides Animal's version: self.speak() on a Dog finds Dog's method first. super().speak() skips to the next class in the MRO, so you can extend rather than replace parent behaviour.

    Calling the same method on a mixed list of Animals and Dogs and getting the right behaviour for each is polymorphism.

    Complexity: O(1) time, O(1) space. Constant-time method dispatch.

    Reveal the reference solution
    class Animal:
        def speak(self):
            return "Some sound"
    
    
    class Dog(Animal):
        def speak(self):
            return "Bark"
    
        def both(self):
            return super().speak(), self.speak()

    Follow-ups interviewers ask

    • Call super().init correctly in a subclass constructor.
    • What does super() do with multiple inheritance?

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Method Overriding and super() in Python?

    The optimal solution runs in O(1) time and O(1) auxiliary space. Constant-time method dispatch.

    What follow-up questions do interviewers ask about Method Overriding and super()?

    Call super().__init__ correctly in a subclass constructor. What does super() do with multiple inheritance?