Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift 2.0 Class Inheritance Overriding Properties

Brian Patterson
Brian Patterson
19,588 Points

This compiles correctly in swift

This compiles correctly in Swift but the challenge says it is wrong, why ?

class Vehicle {
    var numberOfDoors: Int
    var numberOfWheels: Int

    init(withDoors doors: Int, andWheels wheels: Int) {
        self.numberOfDoors = doors
        self.numberOfWheels = wheels

    }
}
class car: Vehicle {
    let numberOfSeats: Int = 4
    override init(withDoors doors: Int, andWheels wheels: Int) {
        super.init(withDoors: doors, andWheels: wheels)

    }
}

let someCar = car.init(withDoors: 4, andWheels: 4)
someCar.numberOfSeats

2 Answers

Bill Siever
Bill Siever
7,649 Points
  1. The class should be named "Car" with a capital C.
  2. You don't need the "init" when constructing an object. Just use the name of the class. That is, let someCar = Car(...)
tonys
tonys
12,963 Points

Bill's spot on with his answer. There's no need to update the init of the subclass either as you're setting a default value for the new variable of seats.

All you need to add is something like:

class Car: Vehicle {
    var numberOfSeats: Int = 4
}

let someCar = Car(withDoors: 2, andWheels: 4)