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

I 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?

Hi,

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.

Ahh makes sense :) thanks!

1 Answer

It helped me by thinking it in layers!

  1. Your first class vehicle has the basic information.
  2. 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!

Ahh no I got the bit about inheritance. It was more to do with having the double init variables.