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 trialEtain Software
5,655 PointsNeed help with subclass init
Cant figure out why this is not working, any help would be great.
class Vehicle {
let wheels: Int
let doors: Int
// Designated initializer
init(wheels:Int, doors:Int){
self.wheels = wheels
self.doors = doors
}
}
class Car: Vehicle {
// A car must default to 4 wheels and 4 doors
let numberOfSeats: Int = 4
override init(){
// call super.init
super.init(wheels: 4, doors: 4)
}
}
let someCar = Car(wheels: 3,doors: 3,numberOfSeats)
2 Answers
Steve Hunter
57,712 PointsHi there,
I just wrote another answer for this quiz, so my subclass and initializer are still on my clipboard ... here it is:
// Enter your code below
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 - if you want an explanation after you've read through it, please shout!
Steve.
Etain Software
5,655 Pointsclass 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: Int, andWheels: Int) {
super.init(withDoors: 4, andWheels: 4)
}
}
let someCar = Car(withDoors: 12, andWheels: 12 )
got a pass with above code. the external parameter threw me off.
Thanks
Steve Hunter
57,712 PointsCool - glad you got it sorted. :-)
dansafaeerad
3,975 Pointsdansafaeerad
3,975 PointsHi,
I was just wondering why self.numberOfSeats = 4 comes before super.init(withDoors: doors, andWheels: wheels)