Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In Swift, to call the parent class's method from an overridden method, use 'super.methodName()'.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
In Swift, the child class must call the parent class initializer using 'super.init(...)' to properly initialize inherited properties.
3fill in blank
hardFix 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'
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'.
✗ Incorrect
To call the parent class's method inside an overridden method, use 'super.methodName()' in Swift.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of 'super' causing recursion.
Using the child class name instead of the parent class name.
✗ Incorrect
Use 'super' to call the parent method.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Assign the parameter 'os' to the property, then call the parent initializer with 'super.init(model: model)'.