5- constructor
5- constructor
Kotlin constructor
Read Discuss Courses Practice
1. Primary Constructor
2. Secondary Constructor
A class in Kotlin can have at most one primary constructor, and one or more
secondary constructors. The primary constructor initializes the class, while the
secondary constructor is used to initialize the class and introduce some extra
logic.
Primary Constructor –
The primary constructor is initialized in the class header, goes after the class
name, using the constructor keyword. The parameters are optional in the
primary constructor.
class Add constructor(val a: Int, val b: Int) {
// code
}
Kotlin
//main function
fun main(args: Array<String>)
{
val add = Add(5, 6)
println("The Sum of numbers 5 and 6 is: ${add.c}")
}
//primary constructor
class Add constructor(a: Int,b:Int)
{
var c = a+b;
}
Output:
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}.
The primary constructor cannot contain any code, the initialization code can be
placed in a separate initializer block prefixed with the init keyword.
Kotlin program of primary constructor with initializer 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 gets called before the constructor of this class is called.
Kotlin
// Member Variables
var name: String
// Initializer Blocks
init{
println("This is first init block")
}
init{
println("This is second init block")
}
init{
println("This is third init block")
}
init {
this.name = _name
println("Name = $name")
}
}
Output:
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.
Kotlin
// initializer block
init {
id = emp_id
name = emp_name
Output:
Secondary Constructor –
//main function
fun main(args: Array<String>)
{
Add(5, 6)
}
//class with one secondary constructor
class Add
{
constructor(a: Int, b:Int)
{
var c = a + b
println("The sum of numbers 5 and 6 is: ${c}")
}
}
Output:
Kotlin
Output:
Kotlin
//main function
fun main(args: Array<String>)
{
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 5, 6 = ${c}")
}
constructor(a: Int, b: Int, c: Int)
{
var d = a + b + c
println("Sum of 5, 6, 7 = ${d}")
}
constructor(a: Int, b: Int, c: Int, d: Int)
{
var e = a + b + c + d
println("Sum of 5, 6, 7, 8 = ${e}")
}
}
Output:
Sum of 5, 6 = 11
Sum of 5, 6, 7 = 18
Sum of 5, 6, 7, 8 = 26
Kotlin
//main function
fun main(args: Array<String>)
{
Add(5,6)
}
class Add {
// calling another secondary using this
constructor(a: Int,b:Int) : this(a,b,7) {
var sumOfTwo = a + b
println("The sum of two numbers 5 and 6 is: $sumOfTwo")
}
// this executes first
constructor(a: Int, b: Int,c: Int) {
var sumOfThree = a + b + c
println("The sum of three numbers 5,6 and 7 is: $sumOfThree")
}
}
Output:
The sum of three numbers 5,6 and 7 is: 18
The sum of two numbers 5 and 6 is: 11
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.
Kotlin
Output: