Google News
logo
Kotlin - Interview Questions
What is the difference between a primary constructor and secondary constructor in Kotlin?
In Kotlin, the primary constructor and secondary constructors serve different purposes and have distinct characteristics. Here are the key differences between the primary constructor and secondary constructors:

1. Declaration :
   * Primary Constructor : The primary constructor is declared as part of the class header, directly after the class name. It can include parameters that serve as properties of the class.
   * Secondary Constructors : Secondary constructors are defined inside the class body using the `constructor` keyword. They provide additional ways to instantiate objects with different parameter sets.

2. Number :
   * Primary Constructor : A class can have only one primary constructor, which is the main constructor of the class.
   * Secondary Constructors : A class can have multiple secondary constructors. They are additional constructors that supplement the primary constructor.

3. Delegation :
   * Primary Constructor : The primary constructor can delegate to another constructor of the same class using the `this` keyword. It allows parameter values to be passed from the primary constructor to secondary constructors or between secondary constructors.
   * Secondary Constructors : Secondary constructors must delegate to the primary constructor or to another secondary constructor in the same class using the `this` keyword. Delegation to the primary constructor is done using the `this` keyword followed by the appropriate constructor call.

4. Initialization :
   * Primary Constructor : The primary constructor can include property declarations directly in its parameter list. It automatically initializes the properties with the provided values.
   * Secondary Constructors : Secondary constructors cannot declare properties directly. They can only perform additional initialization logic but cannot directly initialize properties.
5. Execution :
   * Primary Constructor : The primary constructor is called when an instance of the class is created. It is the entry point for initializing the class and executing any initialization code or `init` blocks.
   * Secondary Constructors : Secondary constructors are called explicitly using the `constructor` keyword. They are invoked when an object is created using the secondary constructor, providing an alternative way to instantiate objects.

6. Code Organization :
   * Primary Constructor : Initialization code and logic related to property initialization are typically placed in `init` blocks associated with the primary constructor. The primary constructor itself does not contain executable code.
   * Secondary Constructors : Secondary constructors can contain executable code directly within their body, allowing for additional initialization or custom logic.

Here's an example that demonstrates the usage of primary and secondary constructors:
class Person(val name: String, val age: Int) {
    // Primary constructor with property declarations

    constructor(name: String) : this(name, 0) {
        // Secondary constructor delegating to the primary constructor with age set to 0
    }

    constructor() : this("", 0) {
        // Secondary constructor delegating to the primary constructor with empty name and age set to 0
    }
}​

In the above example, the `Person` class has a primary constructor with `name` and `age` as parameters. It also has two secondary constructors that delegate to the primary constructor, providing default values for `age` when only the `name` is provided or when no parameters are given.
Advertisement