Purpose of inheritance in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use inheritance in Python, we want to see how the program's running time changes as we add more classes or objects.
We ask: How does the program's work grow when using inheritance?
Analyze the time complexity of the following code snippet.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
n = 10 # example value for n
pets = [Dog() for _ in range(n)]
for pet in pets:
pet.speak()
This code creates a list of Dog objects that inherit from Animal and calls their speak method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of pets and calling speak()
- How many times: Once for each pet, so n times
Each pet in the list calls its speak method once, so the work grows directly with the number of pets.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to speak() |
| 100 | 100 calls to speak() |
| 1000 | 1000 calls to speak() |
Pattern observation: The work increases evenly as the number of pets increases.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of objects using inheritance.
[X] Wrong: "Inheritance makes the program slower because it adds extra steps for each method call."
[OK] Correct: The extra steps are very small and do not multiply with the number of objects; the main time depends on how many times methods are called, not inheritance itself.
Understanding how inheritance affects time helps you explain your code design choices clearly and shows you know how programs grow with more objects.
"What if the speak method called another method inside the parent class? How would the time complexity change?"
Practice
Solution
Step 1: Understand inheritance concept
Inheritance lets one class (child) use code from another class (parent).Step 2: Identify main benefit
This helps reuse code and avoid rewriting the same features.Final Answer:
To allow a class to reuse code from another class -> Option BQuick Check:
Inheritance = Code reuse [OK]
- Confusing inheritance with variable creation
- Thinking inheritance speeds up program execution
- Believing inheritance deletes objects
Dog that inherits from a parent class Animal?Solution
Step 1: Recall Python inheritance syntax
In Python, a child class inherits by putting the parent class name in parentheses after the child class name.Step 2: Match syntax options
Onlyclass Dog(Animal):follows this rule correctly.Final Answer:
class Dog(Animal): -> Option DQuick Check:
Child class syntax = class Child(Parent): [OK]
- Using 'inherits' keyword which doesn't exist in Python
- Using arrow '->' instead of parentheses
- Using colon ':' incorrectly after class name
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self):
return "Bark"
pet = Dog()
print(pet.sound())Solution
Step 1: Understand method overriding in inheritance
TheDogclass has its ownsoundmethod that replaces the one fromAnimal.Step 2: Check which method is called
Whenpet.sound()runs, it uses theDogversion returning "Bark".Final Answer:
Bark -> Option CQuick Check:
Child method overrides parent method = Bark [OK]
- Expecting parent method output instead of child
- Thinking both methods run together
- Assuming error due to method name clash
class Vehicle:
def move(self):
print("Moving")
class Car(Vehicle)
def move(self):
print("Car moving")Solution
Step 1: Check class definition syntax
Python requires a colon ':' at the end of class header lines.Step 2: Identify missing colon
The lineclass Car(Vehicle)misses the colon at the end.Final Answer:
Missing colon after class Car(Vehicle) -> Option AQuick Check:
Class header must end with ':' [OK]
- Forgetting colon after class definition
- Assuming wrong parent class name causes error
- Confusing indentation errors with syntax errors
SmartPhone that inherits features from both Phone and Camera classes. What is the correct way to define SmartPhone to reuse code from both parents?Solution
Step 1: Understand multiple inheritance syntax
Python allows a class to inherit from multiple parents by listing them in parentheses separated by commas.Step 2: Match correct syntax
Onlyclass SmartPhone(Phone, Camera):correctly shows multiple inheritance.Final Answer:
class SmartPhone(Phone, Camera): -> Option AQuick Check:
Multiple inheritance uses commas inside parentheses [OK]
- Using 'inherits' keyword which is invalid
- Trying to separate parents with colon or outside parentheses
- Forgetting parentheses around parent classes
