Constructor | Types of Constructor in Kotlin | Android development

// 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" //
// 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 }
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
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
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
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

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store