Calling super for parent behavior in Swift - Time & Space Complexity
When a method calls super, it runs code from its parent class. We want to see how this affects the time it takes to run.
How does calling super change the work done as input grows?
Analyze the time complexity of the following code snippet.
class Parent {
func process(items: [Int]) {
for item in items {
print(item)
}
}
}
class Child: Parent {
override func process(items: [Int]) {
super.process(items: items)
print("Done")
}
}
This code shows a child class calling its parent's method using super. The parent loops over items, and the child adds a print after.
Look for loops or repeated actions.
- Primary operation: The
forloop in the parent method that prints each item. - How many times: Once for each item in the input array.
As the number of items grows, the loop runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 prints |
| 100 | About 100 prints |
| 1000 | About 1000 prints |
Pattern observation: The work grows directly with the number of items. More items mean more prints.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items.
[X] Wrong: "Calling super adds extra loops or makes the code slower in a complex way."
[OK] Correct: The super call just runs the parent's code once. It doesn't add hidden loops or multiply work beyond what the parent already does.
Understanding how super affects time helps you explain code behavior clearly. It shows you can think about inherited code and its cost, a useful skill in real projects.
What if the child method added its own loop over the items before calling super? How would the time complexity change?
Practice
super.methodName() inside an overridden method in Swift do?Solution
Step 1: Understand method overriding
When a subclass overrides a method, it replaces the parent's method behavior.Step 2: Role of
Callingsupersuper.methodName()runs the original method from the parent class, preserving its behavior.Final Answer:
It runs the parent class's version of the method. -> Option AQuick Check:
Calling super runs parent method [OK]
- Thinking super creates a new method
- Believing super deletes parent methods
- Confusing super with sibling class calls
display() inside an overridden method in Swift?Solution
Step 1: Identify Swift syntax for super calls
Swift uses the keywordsuperfollowed by dot and method name to call parent methods.Step 2: Check each option
Onlysuper.display()matches Swift syntax. Others are invalid or from other languages.Final Answer:
super.display() -> Option AQuick Check:
super.methodName() is correct syntax [OK]
- Using parent instead of super
- Adding self before super
- Using :: like in C++
class Parent {
func greet() {
print("Hello from Parent")
}
}
class Child: Parent {
override func greet() {
print("Child starts")
super.greet()
print("Child ends")
}
}
let c = Child()
c.greet()What is the output when
c.greet() is called?Solution
Step 1: Trace the overridden method
Thegreet()method inChildprints "Child starts", then callssuper.greet()which prints "Hello from Parent", then prints "Child ends".Step 2: Combine outputs in order
Output lines are: "Child starts", "Hello from Parent", "Child ends" in that order.Final Answer:
Child starts Hello from Parent Child ends -> Option BQuick Check:
super call runs parent method in middle [OK]
- Ignoring order of prints
- Assuming super call is last
- Mixing output lines
class Parent {
func show() {
print("Parent show")
}
}
class Child: Parent {
override func show() {
print("Child show")
show() // Line X
}
}What is the problem and how to fix it?
Solution
Step 1: Identify recursion cause
InsideChild.show(), callingshow()calls itself again, causing infinite recursion.Step 2: Correct call to parent method
Usesuper.show()to call the parent class's method and avoid recursion.Final Answer:
Line X causes infinite recursion; fix by calling super.show() instead. -> Option DQuick Check:
Call super.method() to avoid recursion [OK]
- Calling method without super inside override
- Removing override keyword incorrectly
- Changing method name unnecessarily
Grandparent has a method log(), Parent overrides it and calls super.log(), and Child overrides it too. You want Child.log() to add its own message, then call Parent.log(), which calls Grandparent.log(). How should you write Child.log() to achieve this?Solution
Step 1: Understand call chain
Callingsuper.log()inChildcallsParent.log(), which callsGrandparent.log().Step 2: Add Child's message before calling super
To add Child's message first, print it, then callsuper.log().Final Answer:
override func log() { print("Child log") ; super.log() } -> Option CQuick Check:
Print then super.log() for correct order [OK]
- Calling super before print changes order
- Not calling super loses parent logs
- Trying to call Parent.log() directly (not allowed)
Ready to prove it?
Pass the test and this pattern moves unknown
