0

Kotlin – Overloading Constructors

This post demonstrates how to overload class constructors in Kotlin.

Requirements

  • IntelliJ IDEA Ultimate 2016.3
    • The Community Edition may be enough but we have not tried it.
  • Kotlin Version 1.1
  • Windows 10 Enterprise

Primary vs Secondary Constructors

In simplest terms, the type of constructor in Kotlin depends on where we declare/reference the constructor.

Below are examples of primary and secondary constructors. They are equivalent.

Primary Constructor Example

Secondary Constructor Example

Overloading Constructors

For instance, we have a class with overloaded constructors. Note, all classes implicitly extend from java.lang.Object.

Using overloading constructors

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert