required keyword, initializers, and class inheritance

On page https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID231, a page on Initialization, at the section called Required Initializers, I find the following sentence:


"Write the

required
modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer:"

What does it mean to implement the initializer? Does that mean I have to explicitly write code for the inherited initializer, or could I consider the inherited initializers implemented without writing additional code in the subclass by virtue of inheritance? I thought initializers are automatically inherited by subclasses from the superclass by virtue of inheritance, without having to write explicit code for them in the subclass.

Accepted Answer

You need to read the Language Guide more carefully.


Firstly, there are conditions for automatic inheritance of an initializer. If the conditions are met a subclass inherits the initializer. If not it does not.

Secondly, the Guide says clearly: "You do not have to provide an explicit implementation of a required initializer if you can satisfy the requirement with an inherited initializer."

So you need to implement the initializer if you cannot inherit an initializer from the superclass, or if the inherited initilizer would not do exactly what you want.


Jan E.

required keyword, initializers, and class inheritance
 
 
Q