0
0
Swiftprogramming~10 mins

Calling super for parent behavior in Swift - Interactive Code Practice

Choose your learning style9 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.