Nishant Jalan
Nishant Jalan

Reputation: 1038

Calling Secondary constructor to Secondary Constructor in Kotlin

I am new to OOP in Kotlin. I have a strong base in Java. But I am facing this issue which is unresolved.

This is the java code:-

public class Parent {
    String name;
    int age;
    boolean isAlive;

    Parent(String name, int age) {
        this.name = name;
        this.age = age;
    }

    Parent(boolean isAlive) {
        this.isAlive = isAlive;
    }
}

final class Child extends Parent {

    Child(String name, int age) {
        super(name, age);
    }

    Child(boolean isAlive) {
        super(isAlive);
    }
}

I don't know how to write this code in Kotlin. How do you call a parent secondary constructor from the child secondary constructor?

Upvotes: 0

Views: 131

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

Isn't it just

class Child: Parent {
    constructor(name: String, age: Int): super(name, age)
    constructor(isAlive: Boolean): super(isAlive) 
}

?

Upvotes: 4

Related Questions