LifeOS LifeOS - 5 months ago 6x
Swift Question

SWIFT: difference between init and super.init

I am reading The Swift Programming Language book by .

The book says that an Init is an initializer to set up the class when an instance is created. (What I understand is: By creating an instance the code block in init() will be executed).

However the book shows super.init but doesn't tell anything about it.


The official documentation does cover the aspect of initializing a superclass:

The init() initializer for Bicycle starts by calling super.init(), which calls the default initializer for the Bicycle class’s superclass, Vehicle. This ensures that the numberOfWheels inherited property is initialized by Vehicle before Bicycle has the opportunity to modify the property. After calling super.init(), the original value of numberOfWheels is replaced with a new value of 2.

Corresponding example code:


class Vehicle {
    var numberOfWheels = 0
    var description: String {
        return "\(numberOfWheels) wheel(s)"


class Bicycle: Vehicle {
    override init() {
        numberOfWheels = 2