LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Question about Class initializers (Day 12)

Forums > 100 Days of SwiftUI

Hey All,

So I'm a bit confused looking at lessions two and three. In lesson two, we seem to hand-write an super initializer to an instance that is not called in the child class. The parent class contains it's own initializer:

class Employee {
    let hours: Int

    init(hours: Int) {
        self.hours = hours
    }

but the child classes do not:

class Developer: Employee {
    func work() {
        print("I'm writing code for \(hours) hours")
    }

When we create an instance of Developer, Paul shows us calling the initializer to the parent class as part of the instance:

let robert = Developer(hours: 8)

However, in lesson three, he discusses that we must call the parent initializers via super.init. Do we only have to call them if the child class also contains an initializer? If the child class does not contain an initializer and we call it, I presume this creates an issue of infilled values in the parent class since memberwise's arent available?

Thanks, Andrew

2      

Hi!

Do we only have to call them if the child class also contains an initializer?

Short answer is yes. Just as an explanation this is excerpt from book

If a subclass declares any designated initializers of its own, the entire game changes drastically. Now, no initializers are inherited! The existence of an explicit designated initializer blocks initializer inheritance. The only initializers the subclass now has are the initializers that you explicitly write. Moreover, every designated initializer in the subclass now has an extra requirement: it must call one of the superclass’s designated initializers, by saying super.init(...). If it fails to do this, then super.init() is called implicitly if possible.

As a side note, classes unlike structs do not have memberwise initializers. So we always have to use it with classes. You do not have to use init with classes only when you declare its properties and assign them on spot. In that case they have implicit init() like in example

class Dog {
  var name = "Fido"
}

let dog = Dog()

but if you have it like:

class Dog {
 var name: String

  init(name: String) { // You have to use init here
    self.name = name
  }
}

2      

Gotcha. This makes perfect sense. Thank you for your help!

2      

If Yuri's answer helped, please mark your post as Solved!

2      

here is another way of looking at it - if your subclass has a new property, it needs to be initialized as well - so you have to make a new init for the subclass:

class Animal {
    var numberOfLegs = 4

    init(numberOfLegs: Int) {
        self.numberOfLegs = numberOfLegs
    }
    func speak() {

    }
}

and

class Cat : Animal {

    var isTame: Bool

    init (numberOfLegs: Int, isTame: Bool) {
        self.isTame = isTame
        super.init(numberOfLegs: numberOfLegs)
    }
    override func speak() {

    }
}

so we need to reference super.init in the sub-class - hope this helps

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.