Complete the code to declare a base class named Vehicle.
class [1] { var speed: Int = 0 }
The base class is named Vehicle. This is the main class from which others can inherit.
Complete the code to declare a subclass named Car that inherits from Vehicle.
class Car: [1] { var brand: String = "" }
The subclass Car inherits from the base class Vehicle using a colon :.
Fix the error in the subclass initializer to call the base class initializer.
class Vehicle { var speed: Int init(speed: Int) { self.speed = speed } } class Car: Vehicle { var brand: String init(brand: String, speed: Int) { self.brand = brand [1] } }
super.self.init inside the initializer causing recursion.In Swift, the subclass must call the base class initializer using super.init(...) to properly initialize inherited properties.
Fill both blanks to override the base class method and add a new method in the subclass.
class Vehicle { func description() -> String { return "Vehicle moving at speed 0" } } class Car: Vehicle { override func [1]() -> String { return "Car moving at speed 0" } func [2]() -> String { return "This is a car" } }
The method description is overridden in the subclass using override. The new method info is added to provide extra information.
Fill all three blanks to create a subclass that overrides a method and calls the base class method inside it.
class Vehicle { func move() -> String { return "Vehicle is moving" } } class Bike: Vehicle { override func [1]() -> String { let baseMessage = super.[2]() return baseMessage + ", Bike is moving fast" } func [3]() -> String { return "Bike details" } }
The method move is overridden and calls the base class move using super.move(). A new method details is added.