In Java, we're used to having overloaded constructors:
class MyClass {
private final String a;
private final Integer b;
public MyClass(String a) {
this(a, 1);
}
public MyClass(String a, Integer b) {
this.a = a;
this.b = b;
}
}
We can simulate the same behavior in Kotlin:
class MyClass(val a: String, val b: Int, val c: Long) {
constructor(a: String, b: Int) : this(a, b, 0)
constructor(a: String) : this(a, 1)
constructor() : this("Default")
}
But it's usually better to have default parameter values and named arguments instead:
class BetterClass(val a: String = "Default",
val b: Int = 1,
val c: Long = 0)