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 trialjc laurent
6,351 PointsYour definition of Car needs to contain the property specified in the directions
what about this one?
class Vehicle {
var numberOfDoors: Int
var numberOfWheels: Int
var numberOfSeats: Int
init(withDoors doors: Int, andWheels wheels: Int, withSeats seats: Int) {
self.numberOfDoors = doors
self.numberOfWheels = wheels
self.numberOfSeats = seats
}
}
class Car: Vehicle {
override init(withDoors doors:Int,andWheels wheels:Int,withSeats seats:Int) {
super.init(withDoors:doors,andWheels:wheels,withSeats:seats)
self.numberOfSeats = 4
}
}
let someCar = Car(withDoors:0, andWheels: 0, withSeats: 0)
2 Answers
Steve Hunter
57,712 PointsHi there!
You've got a couple of bits that need changing here.
First, put the Vehicle
class back to how it was at the start; don't change Vehicle
, that's your superclass that you want to inherit from. Next, create a subclass called Car
that inherits from Vehicle
:
class Car : Vehicle {}
In that class, define a member variable called numberOfSeats
and make that an Int
datatype. Next up, override the init
method. Here, pass in the two parameters required for the super
which are withDoors
and andWheels
. Inside, assign the value of 4 to numberOfSeats
- you want a default value for the challenge. Then, call super.init
as normal. Last. create an instance and store it in someCar
. You end up with:
class Car : Vehicle {
var numberOfSeats: Int
override init(withDoors doors: Int, andWheels wheels: Int) {
self.numberOfSeats = 4
super.init(withDoors: doors, andWheels: wheels)
}
}
let someCar = Car(withDoors: 4, andWheels: 4)
I hope that helps,
Steve.
jc laurent
6,351 Pointsthks Bud
Steve Hunter
57,712 PointsNo problem!