Calling super within the body of a constructor


#1

Is there any way to do something like this in Kotlin?

open class ParentClass(test: Int) {

    var test: Int = 0

    init  {
        this.test = test + 1
    }

}

class ChildClass : ParentClass {
    
    constructor() {
        super(1)
    }
    
}

I basically want to call super from within the constructor body, so that I can do some calculations on a variable before I pass it into the parent constructor.

This is the closest I’ve come to solving this, and it doesn’t look fantastically elegant.

open class ParentClass(test: Int) {

    var test: Int = 0

    init  {
        this.test = test + 1
    }

}

class ChildClass(test: Int = ChildClass.doSomething()) : ParentClass(test) {

    companion object {
        fun doSomething() : Int {
            return 1
        }
    }

}