Constructor | Types of Constructor in Kotlin | Android development
Hola folks! Hope you have read the OOPs article, if you haven't go and learn Class, Objects, Methods. Click here. After you complete reading this article we will study
Abstract class, Interfaces, Exception Handling, Collections ……..!
A constructor is a special member function that is invoked when an object of the class is created primarily to initialize variables or properties. A class needs to have a constructor and if we do not declare a constructor, then the compiler generates a default constructor.
Kotlin has two types of constructors –
- Primary Constructor
- Secondary Constructor
Toh chaliye jante hain Primary Contructor kaise hote hain!
Constructor basically initializes the objects, primary constructor initializes your objects more easily and we always use it with the class name.
IMPORTANT POINTS:
- Animal is a class name so class animal() here animal() is the primary constructor
- The primary constructor is declared as part of a class header.
- Toh primary constructor hamesha class ke baad likha jaata hain.
// class Animal(val name:String , val color:String) This is basically a constructor which is accepting two values one "string:name" and another "string:color" //
e.g: Case 1:
// class Animal(Val name: String, Val color: String){}fun main () {var animal1 = Animal() // here we are calling the class but, if we won't put any value or argument it will show error as it is mandatory to put the values as we have taken some values in the parent class var animal1 = Animal( name:"tommy ", color:"black") // if you write this way you won't get any error }
Let's see in the below code,
class Animal(Val name: String, Val color: String){}
fun main(){
var animal1 = Animal(name:"tommy",color:"black")
println(animal1.name)
println(animal1.color)
}
Output : Tommy
Black
Case 2 : Again if we add one more object,
class Animal(Val name: String, Val color: String){}
fun main(){
var animal1 = Animal(name:"tommy",color:"black")
println(animal1.name)
println(animal1.color) // here we have added one more object and put values in it
var animal2 = Animal(name:"Jonny",color:"Grey")
println(animal2.name)
println(animal2.color)}
Output : Tommy
Black
Jonny
Grey
Case 3: Now we don't need to write print so many time we will reduce the code by adding the function inside the class.
class Animal(Val name: String, Val color: String){
// here by using primary constructor we have taken the value upwards through object
fun displayDetails()
{
println("Name of animal ${name}")
println("Name of animal ${color}")
println()
}}
fun main(){ // here we are calling the constructor
var animal1 = Animal(name:"tommy",color:"black")
animal1.displayDetails()// here we have added one more objects and put values in it
var animal2 = Animal(name:"Jonny",color:"Grey")
animal2.displayDetails()}
output : Name of Animal Tommy
Color of Animal Black
Name of Animal Jonny
Color of Animal Grey
Case 4: Now we will add initializer block in the class.
class Animal(val name:String , val color:String)
{
// if you have another variable and you want constructor mei jo value aaraha hai wo waale "animalName" variable pe chala jaaye then we can use initializer block
val animalName:String
init{
animalName = name
println(" initalizer block ")
}
fun displayDetails()
{
println("Name of animal ${name}")
println("Name of animal ${color}")
println()
}
}
fun main(){ //1st primary constructor will run
var animal1 = Animal("tommy ","black")
animal1.displayDetails() // then it's value
//then this constructor will get called
var animal2 = Animal("jonny","gray")
}
Output:
initializer block
Tommy
initializer block

Now let's learn Secondary Constructor
So how to initialize a secondary constructor,
Class Apple() // so here inside () we wont add any constructors in this case
We will initialize as :
class Apple
{
// you have to declare the variable globally in order to access them
var color:String
var price:Double constructor( color:String,price:Double) // local variables
{
this.color = color
this.prixe = price
} fun displayAppleDetails() {
println("Color: ${color}")
println("Price: ${price}")
}}
// in order to access this class we need to make some objects fun main() {var apl=Apple(color:"Red,price:123.45)
var ap2=Apple(color:"Blue,price:345.78)
var ap3=Apple(color"Pink",price:3465.67)ap1.DisplayAppleDetails()
ap2.displayAppleDetails()
Ap3.DisplayAppleDetails()}Output:
Color : Red
Price : 123.45
Color : Blue
Price : 345.78
Color : Pink
Price : 3456.67
To conclude, I can say if we are making constructions with class, like class Animal( constructors) it is called primary constructor and when we are creating constructors like,
class Apple {
constructor(color: string,price: Double)
}
It's called secondary constructor.