Init Variable Wasn't in the Parameter?

Hi. This is from the Swift 4.2 Apple book, page 26. How come numberOfSides variable in line 07, wasn't inside the parameter in line 04? If you create an object it'll be:


var testSquare = Square(sideLength: 3, name: testSquare)


Why was there a need to put numberOfSides = 4 on line 7, if you won't be able to put an argument/value for it when you create an object?


Did line 07 here functioned like a get set (which one get or set?) and the value 4 is in memory, ready to use? If it did, why wasn't the normal get set used to store the value 4 for later use?


Also if numberOfSides isn't in Square but is a property in it's superclass NamedShape, why wasn't super init used (like with super.init(name: name))? How's Square telling the compiler that numberOfSides is a property from its NamedShape superclass without the super keyword?


class Square: NamedShape {
    var sideLength: Double
   
    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        numberOfSides = 4
    }
   
    func area() -> Double {
        return sideLength * sideLength
    }
   
    override func simpleDescription() -> String {
        return "A square with sides of length \(sideLength)."
    }
}


Thank you in advance.

God bless, Genesis 1:3

Replies

First of all I want to start from this:

if numberOfSides isn't in Square but is a property in it's superclass NamedShape, why wasn't super init used (like with super.init(name: name))?How's Square telling the compiler that numberOfSides is a property from its NamedShape superclass without the super keyword?


In Swift, as well as in many other object oriented language, you do not use `super` for inherited properties or methods.


class MyBaseClass {
    var aProperty: Int = 0
    func someMethod() {
        print(type(of: self), #function)
    }
}

class MySubClass: MyBaseClass {
    func anotherMethod() {
        aProperty += 1
        someMethod()
        print(aProperty)
    }
}

You do not put `super` when accessing the inherited property `aProperty`.

You do not put `super` when accessing the inherited property `someMethod`.


`super` is required to access the original definition existing in the superclass, when a property or a method is overridden.

(Initializer in Swift is a little bit more complex, but `super` is used in nearly the same way.)


So --

How come numberOfSides variable in line 07, wasn't inside the parameter in line 04?

Line 07 is modifying the inherited property, has no need to be inside the parameter list.


Why was there a need to put numberOfSides = 4 on line 7, if you won't be able to put an argument/value for it when you create an object?

Number of sides of Square is 4, isn't it? But it is initialized to 0 in its superclass.

You need to change the value to 4. It is a constant. You have no need to make it a parameter.


Did line 07 here functioned like a get set (which one get or set?) and the value 4 is in memory, ready to use?

In Swift, when you declare a stored propery, getter and setter are automatically generated.

And when you write an anssignment statement (like `numberOfSides = 4`), the setter is called.


If it did, why wasn't the normal get set used to store the value 4 for later use?

I do not understand what you mean here. But writing an assignment statement is the normal way to call setter in Swift.

Did line 07 here functioned like a get set (which one get or set?) and the value 4 is in memory, ready to use? If it did, why wasn't the normal get set used to store the value 4 for later use?


As OOPer explained, you do use the set that was automatically created.


Maybe a clarification. get and set let you add additional code when value is accessed of set.

That may be used for instance to update an IBOutlet when the value of a property changes, without need to repeat the update everywhere in code.