Bird
Raised Fist0
Swiftprogramming~10 mins

Calling super for parent behavior in Swift - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the parent class's method inside the child class method.

Swift
class Parent {
    func greet() {
        print("Hello from Parent")
    }
}

class Child: Parent {
    override func greet() {
        [1].greet()
        print("Hello from Child")
    }
}
Drag options to blanks, or click blank then click option'
Aself
Bsuper
CParent
DChild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of 'super' to call the parent method.
Calling the method without 'super' leading to recursive calls.
2fill in blank
medium

Complete the code to ensure the parent class initializer is called inside the child class initializer.

Swift
class Vehicle {
    var wheels: Int
    init(wheels: Int) {
        self.wheels = wheels
    }
}

class Car: Vehicle {
    var brand: String
    init(brand: String) {
        self.brand = brand
        [1].init(wheels: 4)
    }
}
Drag options to blanks, or click blank then click option'
Asuper
BCar
CVehicle
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'self.init' instead of 'super.init' causing infinite recursion.
Not calling the parent initializer leading to compile errors.
3fill in blank
hard

Fix the error in the code by correctly calling the parent class's method inside the overridden method.

Swift
class Animal {
    func sound() {
        print("Animal sound")
    }
}

class Dog: Animal {
    override func sound() {
        [1].sound()
        print("Bark")
    }
}

let dog = Dog()
dog.sound()
Drag options to blanks, or click blank then click option'
Aself
BAnimal
Csuper
DDog
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of 'super' causing recursive calls.
Calling the method on the class name instead of using 'super'.
4fill in blank
hard

Fill both blanks to correctly override the method and call the parent class's method.

Swift
class Printer {
    func printMessage() {
        print("Printing from Printer")
    }
}

class ColorPrinter: Printer {
    override func printMessage() {
        [1].printMessage()
        print("Printing in color")
    }
}
Drag options to blanks, or click blank then click option'
Asuper
Bself
CPrinter
DColorPrinter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of 'super' causing recursion.
Using the child class name instead of the parent class name.
5fill in blank
hard

Fill all three blanks to override the initializer and call the parent initializer correctly.

Swift
class Device {
    var model: String
    init(model: String) {
        self.model = model
    }
}

class Phone: Device {
    var os: String
    init(model: String, os: String) {
        self.os = [1]
        [2].init(model: [3])
    }
}
Drag options to blanks, or click blank then click option'
Aos
Bsuper
Cmodel
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of 'super' to call the parent initializer.
Assigning wrong variables to properties.
Not calling the parent initializer causing errors.

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

  1. Step 1: Understand method overriding

    When a subclass overrides a method, it replaces the parent's method behavior.
  2. Step 2: Role of super

    Calling super.methodName() runs the original method from the parent class, preserving its behavior.
  3. Final Answer:

    It runs the parent class's version of the method. -> Option A
  4. 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

  1. Step 1: Identify Swift syntax for super calls

    Swift uses the keyword super followed by dot and method name to call parent methods.
  2. Step 2: Check each option

    Only super.display() matches Swift syntax. Others are invalid or from other languages.
  3. Final Answer:

    super.display() -> Option A
  4. 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

  1. 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".
  2. Step 2: Combine outputs in order

    Output lines are: "Child starts", "Hello from Parent", "Child ends" in that order.
  3. Final Answer:

    Child starts Hello from Parent Child ends -> Option B
  4. 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.

Solution

  1. Step 1: Identify recursion cause

    Inside Child.show(), calling show() calls itself again, causing infinite recursion.
  2. Step 2: Correct call to parent method

    Use super.show() to call the parent class's method and avoid recursion.
  3. Final Answer:

    Line X causes infinite recursion; fix by calling super.show() instead. -> Option D
  4. 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

  1. Step 1: Understand call chain

    Calling super.log() in Child calls Parent.log(), which calls Grandparent.log().
  2. Step 2: Add Child's message before calling super

    To add Child's message first, print it, then call super.log().
  3. Final Answer:

    override func log() { print("Child log") ; super.log() } -> Option C
  4. Quick Check:

    Print then super.log() for correct order [OK]
Hint: Print first, then call super to keep parent behavior [OK]
Common Mistakes:
  • Calling super before print changes order
  • Not calling super loses parent logs
  • Trying to call Parent.log() directly (not allowed)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard