0
0
Swiftprogramming~5 mins

Calling super for parent behavior in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the super keyword do in Swift?
<p>The <code>super</code> keyword calls a method or property from the parent class. It lets a child class use or extend the behavior of its parent.</p>
Click to reveal answer
beginner
Why should you call super when overriding a method?

Calling super ensures the parent class's original behavior runs. This keeps important setup or logic intact while adding new behavior.

Click to reveal answer
beginner
Example: How to call super.viewDidLoad() in a UIViewController subclass?
override func viewDidLoad() {
    super.viewDidLoad()
    // Your code here
}

This calls the parent viewDidLoad() to keep default setup.

Click to reveal answer
intermediate
What happens if you forget to call super in an overridden initializer?

Not calling super.init() can cause your object to be incomplete or crash because the parent class's setup is skipped.

Click to reveal answer
intermediate
Can you call super in a method that is not overridden?

No. You can only call super inside a method that overrides a parent method. Otherwise, the compiler will give an error.

Click to reveal answer
What is the purpose of calling super.method() in Swift?
ATo create a new method in the child class
BTo run the parent class's version of the method
CTo delete the parent class's method
DTo call a method from an unrelated class
What happens if you override viewDidLoad() in a UIViewController subclass but do NOT call super.viewDidLoad()?
AThe app will crash immediately
BThe method will call itself recursively
CNothing, it works fine without calling super
DThe parent setup code will not run, possibly causing issues
Can you call super in a method that does not override a parent method?
ANo, only in overridden methods
BYes, anytime
COnly in initializers
DOnly in static methods
When overriding an initializer in Swift, what must you do to ensure proper setup?
ACall <code>super.init()</code> to run the parent initializer
BCall <code>self.init()</code> instead
CDo nothing special
DCall <code>super.deinit()</code>
What keyword do you use to access the parent class's method in Swift?
Aparent
Bbase
Csuper
Dself
Explain why and how you use super when overriding methods in Swift.
Think about keeping the parent's work while adding your own.
You got /3 concepts.
    Describe what can go wrong if you forget to call super.init() in a subclass initializer.
    Consider what the parent class normally does in its initializer.
    You got /3 concepts.