Learning Kotlin and Swift

Kotlin vs Swift: The Init Construction

Explore Kotlin and Swift together and learn their differences

Photo by Mason Kimbarovsky on Unsplash

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

  1. The init function can have parameters to form a constructor for the class.
  2. Cannot have more than one init() function in the class (i.e. empty parameter constructor)
  3. The init function is called before the parent constructor is called i.e. the parent object is not yet formed.
  4. 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.

--

--