I was learning the following chapter in The Swift Programming Languages:
If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.
Then I tried these codes in my target:
class Car {
var name: String = "Unknown"
init(name: String) {
self.name = name
}
}
class RacingCar: Car {
var speed = 0.0
init(name: String, speed: Double) {
self.speed = speed
super.init(name: name)//this is where I got confused
}
}
According to rule one, RacingCar class won't inherit the init(name:) designated initializer from its superclass Car. However, I'm still able to call the super.init(name:) in my subclass. How could that happen? I'm so confused about this. Can anyone explain why? Thanks.