0
0
Swiftprogramming~10 mins

Calling super for parent behavior in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Calling super for parent behavior
Child method called
Child method runs custom code
Call super.method()
Parent method runs
Return to child method
Child method finishes
When a child class method runs, it can do its own work and then call the parent class method using super to keep the parent's behavior.
Execution Sample
Swift
class Parent {
    func greet() {
        print("Hello from Parent")
    }
}

class Child: Parent {
    override func greet() {
        print("Hello from Child")
        super.greet()
    }
}

let c = Child()
c.greet()
This code shows a child class method calling super to run the parent class method after its own code.
Execution Table
StepActionOutputNotes
1Create Child instance cChild object created
2Call c.greet()Child's greet() method starts
3Print "Hello from Child"Hello from ChildChild prints its message
4Call super.greet()Parent's greet() method called
5Print "Hello from Parent"Hello from ParentParent prints its message
6Return from super.greet()Back to Child's greet()
7Child's greet() finishesMethod ends
💡 Child's greet() method finishes after calling parent's greet() via super
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 7
cnilChild instance createdChild instance existsChild instance exists
Key Moments - 2 Insights
Why do we call super.greet() inside the child's greet() method?
Calling super.greet() runs the parent class's greet() method so the child keeps the parent's behavior in addition to its own, as shown in steps 4 and 5.
What happens if we don't call super.greet() in the child's greet()?
If super.greet() is not called, the parent's greet() method won't run, so only the child's message prints. This is seen by comparing step 5's absence.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 3?
ANothing is printed
B"Hello from Parent"
C"Hello from Child"
D"Hello from Child" and "Hello from Parent"
💡 Hint
Check the Output column at step 3 in the execution_table
At which step does the parent's greet() method run?
AStep 4
BStep 5
CStep 2
DStep 7
💡 Hint
Look for the step where "Hello from Parent" is printed in the execution_table
If we remove super.greet() from the child's greet(), what changes in the output?
AOnly "Hello from Child" prints
BOnly "Hello from Parent" prints
CBoth messages print as before
DNo messages print
💡 Hint
Refer to key_moments explanation about skipping super.greet()
Concept Snapshot
Calling super.method() lets a child class run the parent class's method.
Use it inside an overridden method to keep parent behavior.
Without super, parent's code is skipped.
Example: override func greet() { print("Child"); super.greet() }
Full Transcript
This example shows how a child class method can run its own code and then call the parent class method using super. When c.greet() is called, the child prints "Hello from Child", then calls super.greet() which prints "Hello from Parent". This keeps both behaviors. If super.greet() is omitted, only the child's message prints. The execution table traces each step, showing when each print happens and how the method calls flow.