Industrial Training




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:


  • Primary constructor
  • 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.


class myClass(valname: String,varid: Int) {  
    // class body  
}  

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


class myClass(val name: String, var id: Int) {  
}  
fun main(args: Array){  
val myclass = myClass ("Ashu", 101)  
  
println("Name = ${ myclass.name}")  
println("Id = ${ myclass.id}")  
}  

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:


class myClass(name: String, id: Int) {  
val e_name: String  
var e_id: Int  
init{  
e_name = name.capitalize()  
e_id = id  
  
println("Name = ${e_name}")  
println("Id = ${e_id}")  
    }  
}  
fun main(args: Array){  
val myclass = myClass ("Ashu", 101)  
  
}  


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.


class myClass{  
  
    constructor(id: Int){  
        //code   
    }  
    constructor(name: String, id: Int){  
        //code   
    }  
}  


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


class myClass{  
  
    constructor(name: String, id: Int){  
println("Name = ${name}")  
println("Id = ${id}")  
    }  
}  
fun main(args: Array){  
val myclass = myClass ("Ashu", 101)  
  
}  


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:
class myClass(password: String){  
  
    constructor(name: String, id: Int, password: String): this(password){  
println("Name = ${name}")  
println("Id = ${id}")  
println("Password = ${password}")  
    }  
}  
fun main(args: Array){  
val myclass = myClass ("Ashu", 101, "mypassword")  
  
}  

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:
class myClass{  
  
    constructor(name: String, id: Int): this(name,id, "mypassword"){  
println("this executes next")  
println("Name = ${name}")  
println("Id = ${id}")  
    }  
  
    constructor(name: String, id: Int,pass: String){  
println("this executes first")  
println("Name = ${name}")  
println("Id = ${id}")  
println("Password = ${pass}")  
    }  
}  
fun main(args: Array< String>){  
val myclass = myClass ("Ashu", 101)  
  
}  

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.


open class Parent{  
  
    constructor(name: String, id: Int){  
println("this executes first")  
println("Name = ${name}")  
println("Id = ${id}")  
    }  
  
    constructor(name: String, id: Int,pass: String){  
println("this executes third")  
println("Name = ${name}")  
println("Id = ${id}")  
println("Password = ${pass}")  
    }  
}  
class Child: Parent{  
    constructor(name: String, id: Int): super(name,id){  
println("this executes second")  
println("Name = ${name}")  
println("Id = ${id}")  
    }  
  
   constructor(name: String, id: Int,pass: String):super(name,id,"password"){  
println("this executes forth")  
println("Name = ${name}")  
println("Id = ${id}")  
println("Password = ${pass}")  
    }  
}  
fun main(args: Array< String>){  
val obj1 = Child("Ashu", 101)  
val obj2 = Child("Ashu", 101,"mypassword")  
}  

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



Hi I am Pluto.