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 trialSam Kearney
2,058 PointsClass Inheritance Code Challenge Help
I think the initialiser is what's throwing it off, but I'm not sure what's wrong. Anyone see the issue?
Thanks
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 {
let numberOfSeats: Int = 4
init(withSeats seats: Int) {
self.numberOfSeats = seats
}
}
let someCar = Car(withSeats: 5)
1 Answer
Dylan Glover
2,537 PointsHey Sam, Got this answer from an older question on this challenge, but it passes and seems to explain pretty well. Looks like you were really close!
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
init(numbersOfSeats: Int) {
// We first give values to "numberOfSeats
self.numberOfSeats = numbersOfSeats
// Then we call super init
super.init(withDoors: 2, andWheels: 4)
}
}
let someCar = Car(numbersOfSeats: 2)
Hope this helps!
Dylan
Sam Kearney
2,058 PointsSam Kearney
2,058 PointsI understand it much better now. Thank you!
Dan K
10,689 PointsDan K
10,689 Pointswhy don't you add "doors" and "wheels" when you call super init?