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

Ryan Maneo
Ryan Maneo
4,342 Points

Stuck on class inheritance...

I don't understand... this should work... right?

class Vehicle {

    var wheels: Int
    init(wh: Int) {
        self.wheels = wh
    }


class Car: Vehicle {
 override var wheels: Int = 4
}
}

returns error: cannot override with a stored property 'wheels'

I've tried it many different ways... all return some type of error. And yes, I read the documentation.

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

Could you post a link to the challenge? The one I've found looks quite a bit different. What I can tell you right off the bat though is that you've started a class definition inside your first class definition. Take a close look at the curly braces.

2 Answers

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

To be honest, I'm not that amazing with Swift. And part of that is because my Mac died a few months back. However, I've played some in the Swift sandbox and this is what I came up with. Take a look at this on your machine and see if it works like you're wanting. Hope this helps! :sparkles:

class Vehicle {

    var wheels: Int = 2
    init(wh: Int) {
        self.wheels = wh
    }
}

class Car: Vehicle {
    override init(wh: Int)
    {
        super.init(wh: 4)
        self.wheels = 4
    }
}

let myCar = Car(wh:8)
print(myCar.wheels)

note even if you try to construct the Car with 8 wheels as shown here, it will still be set to 4 :smiley:

Ryan Maneo
Ryan Maneo
4,342 Points

Hm... okay... I guess I have to override the initializer, not the stored property?

Ryan Maneo
Ryan Maneo
4,342 Points

Jennifer Cordell This isn't a challenge actually. I am just trying to understand Classes and Class inheritance and doing private practice... also, That was on purpose... I tried it inside of it, outside of it, inside of a struct, without the initializer, without a value, with a value, as a constant... I just don't understand... is it just me? Ugh!