Javatpoint Logo
Javatpoint Logo

Kotlin Constructor

In Kotlin, constructor is a block of code similar to method. Constructor is declared with the same name as the class followed by parenthesis '()'. Constructor is used to initialize the variables at the time of object creation.

Types of Kotlin constructors

There are two types of constructors in Kotlin:

  1. Primary constructor
  2. Secondary constructor

There is only one primary constructor in a Kotlin class whereas secondary constructor may be one or more.

Kotlin primary constructor

Primary constructor is used to initialize the class. It is declared at class header. Primary constructor code is surrounded by parentheses with optional parameter.

Let's see an example of declaration of primary constructor. In the below code, we declare a constructor myClass with two parameter name and id. Parameter name is only read property whereas id is read and write property.

When the object of myClasss is created, it initializes name and id with "Ashu" and "101" respectively.

Output:

Name = Ashu
Id = 101

Primary constructor with initializer block

The primary constructor does not contain any code. Initializer blocks are used to initialization of code. This block is prefixed with init keyword. At the period of instance initialization, the initialized blocks are executed in the same order as they appear in class body.

Let's rewrite the above code using initialize block:

Output:

Name = Ashu
Id = 101

In above code, parameters name and id accept values "Ashu" and "101" when myclass object is created. The properties name and id are used without "val" or "var", so they are not properties of myClass class.

When object of myClass class is created, it executes initializer block which initializese_name and e_id.

Kotlin secondary constructor

In Kotlin, secondary constructor can be created one or more in class. The secondary constructor is created using "constructor" keyword.

Let's see an example of declaration of secondary constructor. In the below code, we declare two constructor of myClass with two parameter name and id.

Let's see an example of secondary constructor assigning the value while object of class is created.

Output:

Name = Ashu
Id = 101

We can also use both primary as well as secondary constructor in a same class. By using primary as well secondary constructor in same class, secondary constructor needs to authorize to primary constructor. Authorization to another constructor in same class is done using this() keyword.

For example:

Output:

Name = Ashu
Id = 101
Password = mypassword

Calling one secondary constructor from another secondary constructor of same class

In Kotlin, one secondary constructor can call another secondary constructor of same class. This is done by using this() keyword.

For example:

Output:

this executes first
Name = Ashu
Id = 101
Password = mypassword
this executes next
Name = Ashu
Id = 101

Calling supper class secondary constructor from derived class secondary constructor

In Kotlin, one derived class secondary constructor can call the base class secondary constructor. This is done using super keyword, this is the concept of inheritance.

Output:

this executes first
Name = Ashu
Id = 101
this executes second
Name = Ashu
Id = 101
this executes third
Name = Ashu
Id = 101
Password = password
this executes forth
Name = Ashu
Id = 101
Password = mypassword





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA