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

Extra Argument error with Super/Sub classes

I have been playing around in a Playground after completing a few videos on inheritance on the beginner iOS track. I used the example given at the end to create a vehicle superclass and then vehicle type sub classes. The code is as follows:

class Vehicle {
    var wheelCount: Int = 4
    var doorCount: Int = 4
    var license: String
    var topSpeed: Int = 150

    init(wheelCount: Int, doorCount: Int, license: String) {
        self.wheelCount = wheelCount
        self.doorCount = doorCount
        self.license = license
    }
}

class Bus: Vehicle {
    override init(wheelCount: Int, doorCount: Int, license: String) {
        super.init(wheelCount: wheelCount, doorCount: doorCount, license: license) {
            self.topSpeed = 55
        }
    }
}


let newCar = Vehicle.init(wheelCount: 4, doorCount: 4, license: "Standard")
let newBus = Bus.init(wheelCount: 6, doorCount: 2, license: "HGV")
newCar.topSpeed
newBus.topSpeed

However the code does not run with an error around the super.init in the "Bus" class, saying "error: extra argument 'license' in call super.init".

I have no idea at all why this is. I copied the example from the video (tower defence game) and just edited the values to match my vehicle example. Can anyone advise?

Thanks

2 Answers

Those extra braces you added after you call super? Try removing them

class Vehicle {
    var wheelCount: Int = 4
    var doorCount: Int = 4
    var license: String
    var topSpeed: Int = 150

    init(wheelCount: Int, doorCount: Int, license: String) {
        self.wheelCount = wheelCount
        self.doorCount = doorCount
        self.license = license
    }
}
class Bus: Vehicle {

     override init(wheelCount: Int, doorCount: Int, license: String) {
        super.init(wheelCount: wheelCount, doorCount: doorCount, license: license)
        self.topSpeed = 55
    }
} 
let newCar = Vehicle.init(wheelCount: 4, doorCount: 4, license: "Standard")
let newBus = Bus.init(wheelCount: 6, doorCount: 2, license: "HGV")
newCar.topSpeed
newBus.topSpeed

That is correct! Thank you :!