Method overriding in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use method overriding, we replace a method in a child class with a new version.
We want to see how this affects the time it takes to run the program.
Analyze the time complexity of the following code snippet.
class Animal:
def speak(self):
return "Sound"
class Dog(Animal):
def speak(self):
return "Bark"
animal = Animal()
dog = Dog()
print(animal.speak())
print(dog.speak())
This code shows a parent class with a method, and a child class that overrides that method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the
speakmethod on objects. - How many times: Each method is called once in this example.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 method calls |
| 100 | 2 method calls |
| 1000 | 2 method calls |
Pattern observation: The number of operations is constant (2 method calls), regardless of input size n.
Time Complexity: O(1)
This means the time to run is constant; it does not grow with input size in this example.
[X] Wrong: "Overriding a method makes the program slower because it adds extra steps."
[OK] Correct: Overriding just changes which code runs; it does not add extra loops or repeated work by itself.
Understanding how method overriding works helps you explain how programs decide which code to run.
This skill shows you know how object-oriented programs organize and run their tasks efficiently.
"What if the overridden method called the parent method inside it? How would the time complexity change?"
Practice
What is method overriding in Python?
Choose the best description.
Solution
Step 1: Understand method overriding concept
Method overriding means a child class changes a method from its parent by defining a method with the same name.Step 2: Match description to concept
A child class provides a new version of a method from its parent class. correctly describes this behavior, while others describe different concepts.Final Answer:
A child class provides a new version of a method from its parent class. -> Option AQuick Check:
Method overriding = child changes parent method [OK]
- Confusing overriding with overloading
- Thinking overriding means creating a new method
- Believing methods cannot be changed in child classes
Which of the following is the correct way to override a method greet in a child class?
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
# What goes here?
Solution
Step 1: Check method signature for overriding
The child method must have the same name and parameters as the parent method to override it properly.Step 2: Compare options
def greet(self): print("Hello from Child") matches the parent's method signature exactly. def greet(): print("Hello from Child") misses 'self', C adds a parameter, and D returns a string instead of printing.Final Answer:
def greet(self):\n print("Hello from Child") -> Option BQuick Check:
Same method name and parameters = correct override [OK]
- Omitting 'self' parameter in method
- Changing method parameters when overriding
- Returning value instead of matching parent's behavior
What will be the output of the following code?
class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
def sound(self):
print("Bark")
pet = Dog()
pet.sound()Solution
Step 1: Identify method overriding in Dog class
Dog class overrides the sound() method from Animal to print "Bark" instead of "Some sound".Step 2: Check which method is called
When pet.sound() is called, it uses Dog's version, printing "Bark".Final Answer:
Bark -> Option DQuick Check:
Child method called = overridden output [OK]
- Expecting parent method output instead of child's
- Thinking method call causes error
- Confusing print output with return value
Find the error in this code that tries to override a method:
class Vehicle:
def start(self):
print("Vehicle started")
class Car(Vehicle):
def start():
print("Car started")
c = Car()
c.start()Solution
Step 1: Check method signature in child class
Car's start method is missing the 'self' parameter, which is required for instance methods.Step 2: Understand impact of missing 'self'
Without 'self', Python treats start as a static method, causing a TypeError when called on an instance.Final Answer:
Missing 'self' parameter in Car's start method -> Option AQuick Check:
Instance methods need 'self' parameter [OK]
- Forgetting 'self' in child method
- Thinking inheritance causes error
- Misunderstanding method call syntax
Given the classes below, what will be the output when c.describe() is called?
class Parent:
def describe(self):
print("Parent description")
class Child(Parent):
def describe(self):
print("Child description")
super().describe()
c = Child()
c.describe()Solution
Step 1: Understand method overriding with super()
Child's describe() overrides Parent's but calls super().describe() to run parent's method too.Step 2: Trace method calls
Calling c.describe() prints "Child description" first, then calls Parent's describe() printing "Parent description".Final Answer:
Child description\nParent description -> Option CQuick Check:
super() calls parent method after child override [OK]
- Expecting only child's print output
- Thinking super() causes error
- Confusing order of prints
