What is Constructor?
Constructor is a special method which is used to initialize the class object.
In Kotlin we have two type of constructors. primary constructor secondary constructor primary constructor is used to initialize the class. It is declared at class header.
class Employee constructor(name:String){
}
we can also define primary contructor like below
class Employee (name:String){
}
What is init block in Kotlin?
with primary constructor we can just initialize member varibles, if we want to execute on primary constructor, then will use the initilizer block
class Employee (name:String){
init{
println(name)
}
}
fun main(args:Array)
{
var obj=Employee("Chandu");
}
now it will prints name chandu in console. if we define a member variable in Employee class
class Employee (name:String){
var name:String?=null;
init{
(name)
}
}
fun main(args:Array)
{
var obj=Employee("Chandu");
println(obj.name);
}
output: Chandu null
why here obj.name return null?
constructor member can't acces from out side class, so the member vaible of class name was called on object creation and returns null; So How can i assign the value to class member varible? inside init{} block we can assign the constructor variable to class variable like below
class Employee (name:String){
var name:String?=null;
init{
println(name);
this.name=name;
}
}
Now it prints Chandu Chandu
The Priority of class will go like below
primary constructor -> init block -> class variables
Default value constructor
class Employee (name:String,sal:Float=2000.0f){
var name:String?=null;
init{
println(name +", His salary is "+sal);
this.name=name;
}
}
now the output will be
Chandu, His salary is 2000.0
While object creation we are not passing any value for sal argment, so it will take default value 2000.0
Secondary Constructor:
In Any Kotlin classs we can create one or more secondary Constructors
class Employee {
constructor(name:String,sal:Float,ex:Float) {
println(name +", His salary is "+sal+" and experience: $ex");
}
constructor(name:String,sal:Float) {
println(name +", His salary is "+sal);
}
}
fun main(args:Array)
{
Employee("Chandu",1200.0f,12f);
Employee("Chandu",1200.0f);
}
Output
Chandu, His salary is 1200.0 and experience: 12.0
Chandu, His salary is 1200.0
Multiple Secondary Constructors
fun main(args: Array)
{
Add(5, 6)
Add(5, 6, 7)
Add(5, 6, 7, 8)
}
//class with three secondary constructors
class Add
{
constructor(a: Int, b: Int)
{
var c = a + b
println("Sum of 1, 2 = ${c}")
}
constructor(a: Int, b: Int, c: Int)
{
var d = a + b + c
println("Sum of 1, 2, 3 = ${d}")
}
constructor(a: Int, b: Int, c: Int, d: Int)
{
var e = a + b + c + d
println("Sum of 1, 2, 3, 4 = ${e}")
}
}
output
Sum of 1, 2 = 3
Sum of 1, 2, 3 = 6
Sum of 1, 2, 3, 4 = 10
Secondary Constructor Between Parent and Child Classes
fun main(args: Array)
{
Manager(1,"Chandu");
}
open class Employee {
constructor (emp_id: Int, emp_name: String, emp_salary: Double) {
var id: Int = emp_id
var name: String = emp_name
var salary : Double = emp_salary
println("Employee id is: $id")
println("Employee name: $name")
println("Employee salary: $salary")
println()
}
}
class Manager : Employee {
constructor (emp_id : Int, emp_name: String):super(emp_id,emp_name,500000.55){
var id: Int = emp_id
var name: String = emp_name
println("Manager id is: $id")
println("Manager name: $name")
}
}