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
Andrew Taylor
11,500 PointsI don't get this initialisation
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
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 don't get why on this init method you have withDoors and then doors. I get you would normally have a name like "withDoors" and then specify the type :Int. But what I don't get is why you almost chain those two.
Any thoughts?
Andrew Taylor
11,500 PointsAhh makes sense :) thanks!
1 Answer
Nicholas Richardson
4,867 PointsIt helped me by thinking it in layers!
- Your first class vehicle has the basic information.
- then you add another layer car to get more specific information into the class. Overall allows you to make your code very modular.
Hope this helps!
Andrew Taylor
11,500 PointsAhh no I got the bit about inheritance. It was more to do with having the double init variables.
Sathya vegunta
4,061 PointsSathya vegunta
4,061 PointsHi,
withDoors is an external parameter and doors is a local parameter, external parameters is just an alias for local parameters, and external parameters can be used as a description of local parameters, Local Parameters are used to pass the data inside the function , whereas external parameters are used while creating an instance.