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 Object-Oriented Swift 2.0 Class Inheritance Creating a Subclass

Paul Je
Paul Je
4,435 Points

What would I need to change?

Spent over 2 hours trying to understand the code, and trying different variations.. Is that too long? Hopefully I am not studying this material too slow.. I have trouble understanding why you use self, and why the compiler says that I should use withDoors instead of numberOfDoors; doesn't it state that you're already equating them to each other in the syntax? Plus the video didn't mention how you can add another initializer parameter amongst the Doors and Wheels so I am confused on where and how I can add the Seats parameter into the initializer for when I call it. I hope this question isn't too scrambled; any answer will do. Thanks !

classes.swift
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) {
        super.init(withDoors doors: 4, andWheels wheels: 4)
        self.numberOfSeats = seats
    }
}

let someCar = Car(numberOfDoors: 4, numberOfWheels: 4, numberOfSeats: 5)

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I think you're probably doing a lot better than you think you are! But I also feel like you've overworked/overthought this problem. There's no need to do anything with doors and wheels in the Car definition, because those are already set up for you in vehicle. The only thing you're really adding is a number of seats which is going to be 4. That's per the challenge request. Take a look at my solution and see if you can make it match up with the instructions given in the challenge and see how you were over-complicating it.

// Enter your code below

class Car: Vehicle {
  var numberOfSeats: Int = 4
}

let someCar = Car(withDoors: 2, andWheels: 4)

This will make a car "someCar" with 2 doors, 4 wheels and 4 seats. Happy coding!

P.S. The reason it needs withDoors andWheels instead of numberOfDoors and numberOfWheels is because the init inside the Vehicle class is what takes the parameters. It's the withDoors and the andWheels that are the external parameter names. The internal parameter names are doors and wheels. And those are then assigned back to the numberOfWheels and numberOfDoors. Hope that makes sense!

edited for additional information

Paul Je
Paul Je
4,435 Points

Okay great thanks Jennifer!! Really needed the help :)

So is it safe to say that the variables or constants are a placement to leave the values obtained from the initialized values from the init method? Therefore meaning you can't treat the variables as parameters in the init method to pass values onto?