Kotlin Constructors

In Kotlin, you can define a constructor for a class using the constructor keyword. Here's an example of a simple class with a constructor:

class Person constructor(name: String, age: Int) {
    val name: String
    var age: Int

    init {
        this.name = name
        this.age = age
    }

    fun speak() {
        println("My name is $name and I am $age years old.")
    }
}
Sou‮ww:ecr‬w.theitroad.com

In this example, we have defined a class called Person with two properties: name of type String, and age of type Int. We have also defined an initializer block using the init keyword, which sets the values of the name and age properties based on the constructor arguments.

To create an instance of a class with a constructor, you can use the new keyword or simply call the class constructor directly with the required arguments. Here's an example:

val person = Person("Alice", 30)
person.speak()

In this example, we create an instance of the Person class by calling its constructor with the arguments "Alice" and 30. We store the resulting object in a variable called person. We can then call the speak method on the person object to print the message "My name is Alice and I am 30 years old." to the console.

Kotlin also supports primary constructors, which can be defined as part of the class header. Here's an example of a class with a primary constructor:

class Person(val name: String, var age: Int) {
    fun speak() {
        println("My name is $name and I am $age years old.")
    }
}

In this example, we have defined a class called Person with two properties: name of type String, and age of type Int. The val keyword before the name property indicates that it is immutable (i.e., read-only), whereas the var keyword before the age property indicates that it is mutable (i.e., read-write). Because these properties are defined as part of the primary constructor, we do not need to define them separately in an initializer block.

To create an instance of a class with a primary constructor, we can call the class constructor directly with the required arguments, as in the previous example:

val person = Person("Alice", 30)
person.speak()

In addition to the primary constructor, you can define secondary constructors in Kotlin using the constructor keyword. Here's an example:

class Person(val name: String, var age: Int) {
    constructor() : this("", 0) {
    }

    fun speak() {
        println("My name is $name and I am $age years old.")
    }
}

In this example, we have defined a secondary constructor that takes no arguments. The secondary constructor calls the primary constructor with empty strings for the name argument and 0 for the age argument.

You can also define default values for constructor arguments, which can be used to create objects with different sets of properties without having to define multiple constructors. Here's an example:

class Person(val name: String = "", var age: Int = 0) {
    fun speak() {
        println("My name is $name and I am $age years old.")
    }
}

In this example, we have defined default values for the name and age arguments in the primary constructor. This allows us to create instances of the `