Learning Kotlin and Swift
Kotlin vs Swift: The Init Construction
Explore Kotlin and Swift together and learn their differences
Published in
3 min readOct 18, 2020
In Mobile development, the 2 major programming language to learn is Swift and Kotlin. They look very similar, but there are subtle differences.
Let’s look at the one called during construction, the init
that exist in both Kotlin and Swift.
The init function
In Swift, init
is the constructor and is called before the parent is constructed.
In Kotlin, the init
is not really a constructor, and it is called after the parent is constructed.
More details are below.
In Swift
- The
init
function can have parameters to form a constructor for the class. - Cannot have more than one
init()
function in the class (i.e. empty parameter constructor) - The
init
function is called before the parent constructor is called i.e. the parent object is not yet formed. - The
init
function cannot call the class member function unless it’s a static function. The reason is that the parent is not constructed yet, so the child object is not constructed yet.