Challenge - 5 Problems
Swift Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of overridden method call
What is the output of this Swift code when calling
dog.makeSound()?Swift
class Animal { func makeSound() { print("Some generic sound") } } class Dog: Animal { override func makeSound() { print("Bark") } } let dog = Dog() dog.makeSound()
Attempts:
2 left
💡 Hint
Remember that the
override keyword allows a subclass to provide its own implementation of a method.✗ Incorrect
The
Dog class overrides the makeSound() method of Animal. When calling dog.makeSound(), the overridden method in Dog runs, printing "Bark".❓ Predict Output
intermediate2:00remaining
Output of overridden computed property
What will be printed when accessing
circle.description in this Swift code?Swift
class Shape { var description: String { return "A shape" } } class Circle: Shape { override var description: String { return "A circle" } } let circle = Circle() print(circle.description)
Attempts:
2 left
💡 Hint
Check how computed properties can be overridden with
override var.✗ Incorrect
The
Circle class overrides the computed property description from Shape. Accessing circle.description calls the overridden property, printing "A circle".🔧 Debug
advanced2:00remaining
Identify the error in method overriding
Which option correctly explains the error in this Swift code?
Swift
class Vehicle { func startEngine() { print("Engine started") } } class Car: Vehicle { func startEngine() { print("Car engine started") } }
Attempts:
2 left
💡 Hint
In Swift, overriding a method requires the
override keyword.✗ Incorrect
The
Car class tries to override startEngine() but misses the override keyword, causing a compilation error.❓ Predict Output
advanced2:00remaining
Output of property observer in overridden property
What is the output when setting
mySquare.sideLength = 10 in this Swift code?Swift
class Square { var sideLength: Int = 0 { willSet { print("Square will set sideLength to \(newValue)") } } } class ColoredSquare: Square { override var sideLength: Int { get { super.sideLength } set { print("ColoredSquare will set sideLength to \(newValue)") super.sideLength = newValue } } } let mySquare = ColoredSquare() mySquare.sideLength = 10
Attempts:
2 left
💡 Hint
Overridden property observers replace the superclass observers.
✗ Incorrect
The
ColoredSquare overrides the property observer willSet. Only the overridden observer runs, printing "ColoredSquare will set sideLength to 10".🧠 Conceptual
expert2:00remaining
Behavior of overriding a property with different types
Consider this Swift code. What error will occur and why?
Swift
class Parent { var value: Int { return 5 } } class Child: Parent { override var value: String { return "Five" } }
Attempts:
2 left
💡 Hint
Overriding properties must keep the same type in Swift.
✗ Incorrect
Swift requires the overridden property to have the same type as the original. Changing from
Int to String causes a compilation error.