📖 Scenario: Imagine you are creating a simple app with different types of vehicles. Each vehicle can start its engine, but some vehicles have extra steps when starting. You will learn how to use super to call the parent class behavior and add extra steps in child classes.
🎯 Goal: You will build a small Swift program with a parent class Vehicle and a child class Car. You will override a method in the child class and call the parent method using super to keep the original behavior and add new behavior.
📋 What You'll Learn
Create a class called Vehicle with a method startEngine() that prints "Engine started".
Create a subclass called Car that inherits from Vehicle.
Override the startEngine() method in Car and call the parent method using super.startEngine().
Add an extra print statement in Car's startEngine() method that prints "Car is ready to go!".
Create an instance of Car and call its startEngine() method to see both messages.
💡 Why This Matters
🌍 Real World
In real apps, many classes inherit from others and override methods to add or change behavior. Using <code>super</code> helps keep the original behavior while extending it.
💼 Career
Understanding inheritance and calling parent methods is essential for Swift developers working on iOS apps, especially when working with UIKit or SwiftUI where subclassing is common.
Progress0 / 4 steps
1
Create the Vehicle class
Create a class called Vehicle with a method startEngine() that prints exactly "Engine started".
Swift
Hint
Use class Vehicle {} and inside it define func startEngine() that prints the message.
2
Create the Car subclass
Create a subclass called Car that inherits from Vehicle.
Swift
Hint
Use class Car: Vehicle {} to create the subclass.
3
Override startEngine() and call super
In the Car class, override the startEngine() method. Inside it, call the parent method using super.startEngine() and then print exactly "Car is ready to go!".
Swift
Hint
Use override func startEngine() and inside call super.startEngine() before printing the extra message.
4
Create Car instance and call startEngine()
Create an instance of Car called myCar and call its startEngine() method to print both messages.
Swift
Hint
Create let myCar = Car() and then call myCar.startEngine().
Practice
(1/5)
1. What does calling super.methodName() inside an overridden method in Swift do?
easy
A. It runs the parent class's version of the method.
B. It creates a new method unrelated to the parent.
C. It deletes the parent class's method.
D. It calls a method from a sibling class.
Solution
Step 1: Understand method overriding
When a subclass overrides a method, it replaces the parent's method behavior.
Step 2: Role of super
Calling super.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 A
Quick Check:
Calling super runs parent method [OK]
Hint: Remember: super calls parent method inside override [OK]
Common Mistakes:
Thinking super creates a new method
Believing super deletes parent methods
Confusing super with sibling class calls
2. Which of the following is the correct syntax to call a parent class's method named display() inside an overridden method in Swift?
easy
A. super.display()
B. parent.display()
C. self.super.display()
D. super::display()
Solution
Step 1: Identify Swift syntax for super calls
Swift uses the keyword super followed by dot and method name to call parent methods.
Step 2: Check each option
Only super.display() matches Swift syntax. Others are invalid or from other languages.
Final Answer:
super.display() -> Option A
Quick Check:
super.methodName() is correct syntax [OK]
Hint: Use super.methodName() to call parent method [OK]
Common Mistakes:
Using parent instead of super
Adding self before super
Using :: like in C++
3. Consider this Swift code:
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?
medium
A. Child starts\nChild ends\nHello from Parent
B. Child starts\nHello from Parent\nChild ends
C. Hello from Parent
D. Hello from Parent\nChild starts\nChild ends
Solution
Step 1: Trace the overridden method
The greet() method in Child prints "Child starts", then calls super.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 B
Quick Check:
super call runs parent method in middle [OK]
Hint: Follow print order: before, super call, after [OK]
Common Mistakes:
Ignoring order of prints
Assuming super call is last
Mixing output lines
4. This Swift code tries to override a method but causes an error:
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?
medium
A. Missing super keyword before override.
B. Method name mismatch; rename show() in Child.
C. Cannot override non-virtual method; remove override keyword.
D. Line X causes infinite recursion; fix by calling super.show() instead.
Use super.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 D
Quick Check:
Call super.method() to avoid recursion [OK]
Hint: Use super.method() inside override to avoid recursion [OK]
Common Mistakes:
Calling method without super inside override
Removing override keyword incorrectly
Changing method name unnecessarily
5. You have a class hierarchy where 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?
hard
A. override func log() { print("Child log") }
B. override func log() { super.log() ; print("Child log") }
C. override func log() { print("Child log") ; super.log() }
D. override func log() { Parent.log() ; print("Child log") }
Solution
Step 1: Understand call chain
Calling super.log() in Child calls Parent.log(), which calls Grandparent.log().
Step 2: Add Child's message before calling super
To add Child's message first, print it, then call super.log().