Parent and child classes in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using parent and child classes, it's important to see how the program runs as the number of objects grows.
We want to know how the time to run changes when we create or use many class instances.
Analyze the time complexity of the following code snippet.
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
def greet(self):
super().greet()
print("Hello from Child")
children = [Child() for _ in range(n)]
for child in children:
child.greet()
This code creates a list of child objects and calls a method on each one that also calls the parent method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of child objects and calling the greet method on each.
- How many times: The loop runs once for each child object, so n times.
As the number of child objects increases, the total work grows in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 greet calls |
| 100 | About 100 greet calls |
| 1000 | About 1000 greet calls |
Pattern observation: Doubling the number of objects doubles the total calls and work done.
Time Complexity: O(n)
This means the time to run grows directly with the number of child objects created and used.
[X] Wrong: "Calling a method on child classes that use parent methods makes the program slower in a way that grows faster than the number of objects."
[OK] Correct: Each method call is simple and happens once per object, so the total time grows just with the number of objects, not faster.
Understanding how loops over class instances affect time helps you explain object-oriented code performance clearly and confidently.
"What if the greet method called itself recursively inside the child class? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of parent class
A parent class is designed to hold common features like methods and attributes that multiple child classes can share.Step 2: Compare options with this role
To hold common features that child classes can inherit correctly states this purpose. Other options describe incorrect or unrelated roles.Final Answer:
To hold common features that child classes can inherit -> Option AQuick Check:
Parent class = common features [OK]
- Thinking parent classes prevent changes in children
- Believing parent classes are only for direct instances
- Confusing overriding with inheritance
Dog that inherits from a parent class Animal?Solution
Step 1: Recall Python inheritance syntax
In Python, a child class inherits from a parent class by placing the parent class name in parentheses after the child class name.Step 2: Match options with correct syntax
class Dog(Animal): usesclass Dog(Animal):which is correct. Other options use incorrect keywords or symbols not valid in Python.Final Answer:
class Dog(Animal): -> Option DQuick Check:
Child class syntax = class Child(Parent): [OK]
- Using 'inherits' or 'extends' keywords (not Python)
- Using symbols like '<' instead of parentheses
- Omitting the colon at the end
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
def greet(self):
return "Hello from Child"
obj = Child()
print(obj.greet())Solution
Step 1: Understand method overriding in child class
The child classChilddefines its owngreetmethod, which replaces the parent's method when called on a child instance.Step 2: Check which method is called
Sinceobjis an instance ofChild, callingobj.greet()uses the child's method, returning "Hello from Child".Final Answer:
Hello from Child -> Option CQuick Check:
Child method overrides parent method = Hello from Child [OK]
- Thinking parent method runs instead of child's
- Expecting combined output from both methods
- Assuming method not found error
class Parent:
def __init__(self, name):
self.name = name
class Child(Parent):
def __init__(self, name, age):
self.age = age
c = Child('Anna', 10)
print(c.name, c.age)Solution
Step 1: Check constructor chaining in child class
The child class__init__method setsagebut does not callsuper().__init__(name)to setnamefrom the parent.Step 2: Understand consequence of missing super call
Becausenameis not set inChild, accessingc.namewill cause an error or missing attribute.Final Answer:
Child class __init__ does not call Parent __init__, so name is missing -> Option AQuick Check:
Missing super() call = missing parent attributes [OK]
- Forgetting to call super().__init__ in child constructor
- Assuming parent attributes set automatically
- Confusing syntax errors with logic errors
print(c.describe()) output?class Parent:
def describe(self):
return "I am a parent"
class Child(Parent):
def describe(self):
parent_desc = super().describe()
return parent_desc + " and I am a child"
c = Child()Solution
Step 1: Understand super() usage in child describe method
The child method callssuper().describe()which runs the parent method returning "I am a parent".Step 2: Combine parent and child strings
The child method adds " and I am a child" to the parent's string, so the full return is "I am a parent and I am a child".Final Answer:
I am a parent and I am a child -> Option BQuick Check:
super() calls parent method, combined output [OK]
- Expecting only child or only parent output
- Thinking super() causes error without arguments
- Ignoring string concatenation
