In this tutorial, we will learn about primary and secondary constructors in Kotlin with examples. A primary constructor is part of the header. A class can have multiple secondary constructors. As we all know constructors are the only way to initialize a class.

I would advise to read about Kotlin inheritance first then resume this tutorial.

To understand this concept in details we need two classes. Let’s say the first class is Animal and the second class is a Dog class.

Animal and Dog class.

open class Animal { open var color = "" } class Dog() : Animal() { var breed = "" }

Inside the Animal class we have a color property and inside the Dog class, we have breed property. The Dog class is derived from Animal. So we have the color property inside the Dog class as well.

Let’s create an instance of Dog object inside the main function.

fun main(args: Array<String>) { val dog = Dog() dog.color="black" dog.breed="labrador" }

We are assigning values to color and breed properties. This is the normal scenario.

In Kotlin, we can declare the breed property inside the Dog primary constructor and can also assign a default value.

class Dog(var breed:String="labrador") : Animal() { }

Now if we don’t pass any value for breed it will get “labrador” by default. If we pass any value it will override the default value. It is the same concept that we have studied in Kotlin default parameter function.

We can apply the same concept in the parent class primary constructor as well. Here is the code.

open class Animal(var color: String) { }

But in the case, the derived class constructor will show an error.

Error:(3, 19) Kotlin: No value passed for parameter ‘breed’

To solve this error we have to modify the Dog class, primary constructor. We have to pass the color argument to the parent class via the child class.

class Dog(color:String, var breed: String) : Animal(color) { }

We are calling the Animal class constructor and passing the color parameter. Here is the complete code.

fun main(args: Array<String>) { val dog = Dog("black", "labrador") } open class Animal(var color: String) { } class Dog(color: String, var breed: String) : Animal(color) { }

Kotlin Primary Constructor Example

So what is happening here? From the main function, the Dog class constructor gets called. The value “black” is passed to the color and “labrador” to breed parameter. Then we are calling the superclass constructor and passing the color parameter. The value “black” will be passed to the Animal class.

Let me show you by printing the values inside in the init block .

fun main(args: Array<String>) { val dog = Dog("black", "labrador") } open class Animal(var color: String) { init { println("init Animal: color is $color ") } } class Dog(color: String, var breed: String) : Animal(color) { init { println("init Dog: breed is $breed ") } }

The output.

init Animal: color is black init Dog: breed is labrador

First, the superclass primary constructor gets executed then the derived class primary constructor. Remember the order if you have any primary constructor in Kotlin.

Kotlin Secondary Constructor Example

Let me show you Kotlin secondary constructor example. We have to define the secondary constructor. The keyword for the secondary constructor is constructor. I am going to modify the Animal class code.

open class Animal { var color = "" constructor(color: String) { this.color = color } }

We cannot define properties inside the secondary constructor as we did inside the primary constructor. So we have to explicitly define the properties inside the class.

Now it time to modify the Dog class .

class Dog : Animal { var breed: String = "" constructor(color: String, breed: String) : super(color) { this.breed = breed } }

In the Dog class have a secondary constructor. Using the super keyword we are calling the superclass constructor and passing the color parameter.

Let me show you the complete code by adding some print lines and calling from the main function.

fun main(args: Array<String>) { val dog = Dog("black", "labrador") } open class Animal { var color = "" constructor(color: String) { this.color = color println("Animal Secondary constructor: color is $color ") } } class Dog : Animal { var breed: String = "" constructor(color: String, breed: String) : super(color) { this.breed = breed println("Dog Secondary constructor: breed is $breed ") } }

The output is

Animal Secondary constructor: color is black Dog Secondary constructor: breed is labrador

Again from Dog constructor we are calling the Animal constructor first. We can verify it from the output as well.

This is all from Kotlin primary and secondary constructor with examples. I hope you have learned something new.