concept subclass in category swift

appears as: subclasses, subclass, subclass, A subclass
Swift in Depth

This is an excerpt from Manning's book Swift in Depth.

Subclassing allows you to build a hierarchy of your data. For example, you could have a fast food restaurant selling burgers, fries, the usual. For that, you’d create a superclass of FastFood, with subclasses like Burger, Fries, and Soda.

This chapter sheds some light on the situation so that instead of having a boxing match with the compiler, you can make the most out of initializing structs, classes, and subclasses.

Subclasses can now replace override with required in the related initializer.

Listing 5.24. Subclass required
class MutabilityLand: BoardGame {

      // ... snip

    convenience required init(players: [Player], numberOfTiles: Int) {
        self.init(players: players, instructions: "Read the manual",
     numberOfTiles: numberOfTiles)
    }
}

Now the compiler is happy, and you can reap the benefits of factory methods on superclasses and subclasses.

  • If a subclass has its own stored properties, it won’t directly inherit its superclass initializers.