Kotlin calling non final function in constructor works : <p>In Kotlin, it warns you when calling an abstract function in a constructor, citing the following problematic code:</p>

<pre><code>abstract class Base {
    var code = calculate()
    abstract fun calculate(): Int
}

class Derived(private val x: Int) : Base() {
    override fun calculate(): Int = x
}

fun main(args: Array&lt;String&gt;) {
    val i = Derived(42).code // Expected: 42, actual: 0
    println(i)
}
</code></pre>

<p>And the output makes sense because when <code>calculate</code> is called, <code>x</code> hasn't been initialized yet.</p>

<p>This is something I had never considered when writing java, as I have used this pattern without any issues:</p>

<pre><code>class Base {

    private int area;

    Base(Room room) {
        area = extractArea(room);
    }

    abstract int extractArea(Room room);
}

class Derived_A extends Base {

    Derived_A(Room room) {
        super(room);
    }

    @Override
    public int extractArea(Room room) {
        // Extract area A from room
    }
}

class Derived_B extends Base {

    Derived_B(Room room) {
        super(room);
    }

    @Override
    public int extractArea(Room room) {
        // Extract area B from room
    }
}
</code></pre>

<p>And this has worked fine because the overriden <code>extractArea</code> functions don't rely on any uninitialized data, but they are unique to each respective derived <code>class</code> (hence the need to be abstract). This also works in kotlin, but it still gives the warning.</p>

<p>So is this poor practice in java/kotlin? If so, how can I improve it? And is it possible to implement in kotlin without being warned about using non-final functions in constructors?</p>

<p>A potential solution is to move the line <code>area = extractArea()</code> to each derived constructor, but this doesn't seem ideal since it's just repeated code that should be part of the super class.</p>
