Constructor overload
In Java, we're used to having overloaded constructors. For example, let's look at the following Java class, which requires the a
parameter and defaults the value of b
to 1
:
class User { private final String name; private final boolean resetPassword; public User(String name) { this(name, true); } public User(String name, boolean resetPassword) { this.name = name; this.resetPassword = resetPassword; } }
We can simulate the same behavior in Kotlin by defining multiple constructors using the constructor
keyword:
class User(val name: String, val resetPassword: Boolean) { constructor(name: String) : this(name, true) }
The secondary...