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 Creating a Subclass

Kawal Oberoi
Kawal Oberoi
6,429 Points

Code not working ;-;

Happening so often, don't know where I go wrong.

classes.swift
class Vehicle {
    var numberOfDoors: Int
    var numberOfWheels: Int

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

// Enter your code below

class Car: Vehicle {
    var numberofSeats : Int = 4
    override init (withDoors doors: Int, andWheels wheels: Int){
        super.init(withDoors: doors, andWheels: wheels)
    }
}

let someCar = Car(withDoors: 3, andWheels: 5)

3 Answers

Stone Preston
Stone Preston
42,016 Points

you dont need to override the init method since it will use the same method as the super class. Thats the beauty of inheritance. Since the method is the same as the super class, we dont need to re write it. You also need to make sure you use the correct capitalization of the property name that the challenge asks for

class Vehicle {
    var numberOfDoors: Int
    var numberOfWheels: Int

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

// Enter your code below

class Car: Vehicle {
    var numberOfSeats : Int = 4

}

let someCar = Car(withDoors: 3, andWheels: 5)
Greg Kaleka
Greg Kaleka
39,021 Points

TYPO!

Haha stupid typos... Just make sure you're properly using camelCase for your new property, numberOfSeats. The rest of your code looks good!

Edit: As Stone mentioned, you don't need to override the init method if you're not changing anything about it, so feel free to drop that method from your code as well.

So vivid i was having the same problem