Swift - Write program to Create a subclass of Motor Vehicle named Truck

Requirements

Given the following class

enum Color: String {
       case Red = "Red"
       case Blue = "Blue"
       case White = "white"
}

class Vehicle {
       var model: String
       var doors: Int
       var color: Color
       var wheels: Int

       init() {
         model = ""
         doors = 0
         color = Color.White
         wheels = 0
     }
}

class MotorVehicle: Vehicle {
     var licensePlate: String

     override init() {
         licensePlate = "NOT ASSIGNED"
         super.init()
     }
}

Write program to Create a subclass of Motor Vehicle named Truck

Create the following initializers:

  • an initializer that sets doors to 2
  • an initializer that initializes the model , doors , color , and wheels
  • a convenience initializer that initializes licensePlate and calls the initializer that initializes the model , doors, color , and wheels
class Truck: MotorVehicle {
     override init() {
         super.init()
         doors = 2
     }

     init(model:String, doors:Int, color:Color, wheels: Int) {
         super.init()
         self.model = model
         self.doors = doors
         self.color = color
         self.wheels = wheels
     }

     convenience init(licensePlate:String) {
         self.init(model:"", doors:2, color:Color.White, wheels:2)
         self.licensePlate = licensePlate
     }
}

Related Exercise