Left Index

Kotlin Constructor

A constructor is a special member function that is invoked when an object of the class is created primarily to initialize variables or properties. In Kotlin there are two types of constructors

  • Primary Constructor
  • Secondary constructor

Primary Constructor

In Kotlin, primary constructor create in class header. In below syntax, show you two primary constructor. One is empty constructor and second is parameterized constructor where we pass some parameters. Secondary constructor will be called is decided by the compiler based on the arguments received. We does not specify to invoke which constructor and compiler decides by itself.


We can create multiple constructors accordingly.

Syntax:
// empty primary constructor
class className() { // class header
      // class body
}

// or
// empty primary constructor
class className(paramerters) { // class header
      // class body
}

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap
import com.sun.xml.internal.ws.api.pipe.Engine

fun main(args: Array<String>) {
	var student = Student("Jony", 20)
	
	println("Name: ${student.name}")
	println("Roll No.: ${student.rollNo}")
}

class Student(studentName: String, studentRollNo: Int) {
	val name: String
	val rollNo: Int
	
	// can init properties into init block
	init {
		name = studentName;
		rollNo = studentRollNo
	}
}
Output:
Name: Jony
Roll No.: 20

Note: We can initialize properties at declaration time without init block, as shown below

Example:
class Student(studentName: String, studentRollNo: Int) {
	val name: String = studentName
	val rollNo: Int = studentRollNo
}

Secondary constructor

In Kotlin, a class can also contain one or more secondary constructors. They are created using constructor keyword. Secondary constructor will be called is decided by the compiler based on the arguments received. We does not specify to invoke which constructor and compiler decides by itself.


We can create multiple constructors accordingly.

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap
import com.sun.xml.internal.ws.api.pipe.Engine

fun main(args: Array<String>) {
	var student = Student("Jony", 20)
	
	println("Name: ${student.name}")
	println("Roll No.: ${student.rollNo}")
}


class Student{
	val name: String
	val rollNo: Int

	// default constructor
	constructor() {
		name = "Unknown"
		rollNo = 1
	}
	
	constructor(name: String) {
		this.name = name
		this.rollNo = 1
	}
	
	constructor(rollNo: Int) {
		this.name = "Unknown"
		this.rollNo = rollNo
	}
	
	constructor(name: String, rollNo: Int) {
		this.name = name;
		this.rollNo = rollNo
	}
}
Output:
Name: Jony
Roll No.: 20

Calling one Constructor from another

In Kotlin, you can also call a constructor from another constructor of the same class (like in Java) using this()

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap
import com.sun.xml.internal.ws.api.pipe.Engine

fun main(args: Array<String>) {
	var student = Student("Jony", 20)
	
	println("Name: ${student.name}")
	println("Roll No.: ${student.rollNo}")
}


class Student{
	val name: String
	val rollNo: Int

	// default constructor
	constructor(): this("Unknown", 1) {
	}
	
	constructor(name: String): this(name, 1) {
	}
	
	constructor(rollNo: Int): this("Unknown", rollNo) {
	}
	
	constructor(name: String, rollNo: Int) {
		this.name = name;
		this.rollNo = rollNo
	}
}
Output:
Name: Jony
Roll No.: 20

Calling parent class constructor

Like java, In Kotlin we can call primary class constructor using super keyword as shown below code.


Note: by default you create class is final means that can not be inherited, so we have to create class with open keyword so that we can inherited class.

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap
import com.sun.xml.internal.ws.api.pipe.Engine

fun main(args: Array<String>) {
	var student = Student("Jony", 20, "Jack Danial", "Monalisa")
	
	println("Name: ${student.name} s/o ${student.fatherName} & Mother Name: ${student.motherName}")
	println("Roll No.: ${student.rollNo}")
}


open class Parents {
	var fatherName: String
	var motherName: String
	
	constructor(fatherName: String, motherName: String) {
		this.fatherName = fatherName
		this.motherName = motherName
	}
}

class Student : Parents {
	var name: String
	var rollNo: Int
	
	constructor(name: String, rollNo: Int, fatherName: String, motherName: String): super(fatherName, motherName) {
		this.name = name
		this.rollNo = rollNo
	}
}
Output:
Name: Jony s/o Jack Danial & Mother Name: Monalisa
Roll No.: 20