Open In App

Kotlin Constructor

Last Updated : 01 Jun, 2025
Suggest changes
Share
Like Article
Like
Report

A constructor is a special member function that is automatically called when an object of a class is created. Its main purpose is to initialize properties or perform setup operations. In Kotlin, constructors are concise, expressive, and provide significant flexibility with features like default parameters, initializer blocks, and multiple constructor definitions.

Types of Constructors in Kotlin

Kotlin supports two types of constructors:

  1. Primary Constructor
  2. Secondary Constructor

Note: A class can have only one primary constructor, but multiple secondary constructors.

Primary Constructor

The primary constructor is defined directly in the class header, after the class name. It may include parameters to initialize properties.

class ClassName constructor(param1: Type, param2: Type) {
// class body
}

The constructor keyword can be omitted if there is no annotations or access modifiers specified.  

class Add(val a: Int, val b: Int) {
// class body
}

Example of primary constructor:

Kotlin
class Add(val a: Int, val b: Int) {  val c = a + b } fun main() {  val add = Add(5, 6)  println("The Sum of two numbers is: ${add.c}") } 

Output: 

The Sum of two numbers is: 11

Explanation:

When we create the object add for the class then the values 5 and 6 passes to the constructor. The constructor parameters a and b initialize with the parameters 5 and 6 respectively. The local variable c contains the sum of variables. In the main, we access the property of constructor using ${add.c}.

Initializer Block (init block)

The primary constructor cannot contain any logic. To execute code during object initialization, Kotlin provides the init block.

Syntax:

class Person(val name: String) {
val id: Int = 101

init {
println("Initializing with name: $name")
}
}

What is init block? 

It acts as an initialiser block where member variables are initialised. This block gets executed whenever an instance of this class is created. There can be multiple init blocks and they are called in the order they are written inside the class.

Note: init blocks are called in the order they appear, before the class body is executed.

Kotlin
class Person(val name: String) {  init { println("This is first init block") }  init { println("This is second init block") }  init { println("This is third init block") } } fun main() {  val person = Person("Geeks")  println("Name = ${person.name}") } 

Output: 

This is first init block
This is second init block
This is third init block
Name = Geeks

Explanation:

When the object person is created for the class Person, the value "Mahmood" is passed to the parameters name of the constructor. Two properties are declared in the class id and name.

Initializer block is executed at the time of object creation, and not only initializes the properties but also prints to the standard output.

Default Parameter Values in Primary Constructor

Similar to function default values in functions, we can initialize the constructor parameters with some default values.

Example of default values in primary constructor:

Kotlin
class Employee(val empId: Int = 100, val empName: String = "abc") fun main() {  val emp1 = Employee(18018, "Sagnik")  val emp2 = Employee(11011)  val emp3 = Employee()  println("Employee id is: ${emp1.empId}, Employee name: ${emp1.empName}")  println("Employee id is: ${emp2.empId}, Employee name: ${emp2.empName}")  println("Employee id is: ${emp3.empId}, Employee name: ${emp3.empName}") } 

Output: 

Employee id is: 18018, Employee name: Sagnik
Employee id is: 11011, Employee name: abc
Employee id is: 100, Employee name: abc

Explanation:

Here, we have initialized the constructor parameters with some default values emp_id = 100 and emp_name = "abc". When the object emp is created we passed the values for both the parameters, so it prints those values. 
But, at the time of object emp2 creation, we have not passed the emp_name so initializer block uses the default values and print to the standard output. 

Secondary Constructor

A secondary constructor is declared using the constructor keyword inside the class body. We can define one or more secondary constructors to include custom initialization logic.

Syntax:

class MyClass {
constructor(param1: Type, param2: Type) {
// constructor body
}
}

Example of secondary constructor:

Kotlin
class Add {  var c: Int = 0  constructor(a: Int, b: Int) {  c = a + b  println("The sum of numbers $a and $b is: $c")  } } fun main() {  val add = Add(5, 6) } 

Output: 

The sum of numbers 5 and 6 is: 11

Which secondary constructor will be called is decided by the compiler based on the arguments received. In the above program, we do not specify to invoke which constructor and compiler decides by itself.

Multiple Secondary Constructors

We can define multiple secondary constructors to provide different ways of creating objects.

Example of multiple secondary constructors in a class:

Kotlin
class Employee {  constructor(empId: Int, empName: String) {  println("Employee id is: $empId, Employee name: $empName")  }  constructor(empId: Int, empName: String, salary: Double) {  println("Employee id is: $empId, Employee name: $empName, Salary: $salary")  } } fun main() {  val e1 = Employee(18018, "Sagnik")  val e2 = Employee(11011, "Praveen", 600000.5) } 

Output: 

Employee id is: 18018, Employee name: Sagnik
Employee id is: 11011, Employee name: Praveen, Salary: 600000.5

Calling one secondary constructor from another

A secondary constructor may call another secondary constructor of the same class using this() function. In the below program, we have called another constructor using this(a,b,7) because invoking of that constructor requires three parameters.

Example of calling one constructor from another:

Kotlin
class Add {  constructor(a: Int, b: Int, c: Int) {  println("Sum of $a, $b, $c = ${a + b + c}")  }  constructor(a: Int, b: Int) : this(a, b, 7) {  println("Sum of two numbers $a and $b is: ${a + b}")  } } fun main() {  val add = Add(5, 6) } 

Output:

Sum of 5, 6, 7 = 18
Sum of two numbers 5 and 6 is: 11

Calling Parent Class Constructor from Child Class

We can call the secondary constructor of parent class from the child class using the super keyword. In the below program, we have shown the process of calling. 

As we can see here is a new keyword called open, an open class is an ordinary class that is open for extension. By default, when we write a class in Kotlin, it cannot be extended. Yes, inheritance is prevented by default. By declaring a class to be open, we tell the compiler: "I intend to extend this class".

Kotlin
open class Employee {  constructor(empId: Int, empName: String) {  println("Employee id is: $empId")  println("Employee name: $empName")  } } class Manager : Employee {  constructor(empId: Int, empName: String, salary: Double) : super(empId, empName) {  println("Employee salary: $salary")  } } fun main() {  val manager = Manager(18018, "Sagnik", 500000.55) } 

Output: 

Employee id is: 18018
Employee name: Sagnik
Employee salary: 500000.55

Next Article

Similar Reads

Article Tags :