Error in Swift class: Property not initialized at super.init call

I have two classes, Shape and Square

class Shape {
    var numberOfSides = 0
    var name: String
    init(name:String) {
        self.name = name
    }
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

class Square: Shape {
    var sideLength: Double

    init(sideLength:Double, name:String) {
        super.init(name:name) // Error here
        self.sideLength = sideLength
        numberOfSides = 4
    }
    func area () -> Double {
        return sideLength * sideLength
    }
}

With the implementation above I get the error:

property 'self.sideLength' not initialized at super.init call
    super.init(name:name)

Why do I have to set self.sideLength before calling super.init?

12 Answers
12

Leave a Comment