LifeOS LifeOS - 1 year ago 57
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.

Answer Source

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