I want to know why exactly my code returns "nullnullnull" instead of expected answer and why does it work correctly when method is used.
Consider the following code:
open class Base( open val a: String, open val b: String, open val c: String, ) { val concatenation = a + b + c } class Derived( override val a: String, override val b: String, override val c: String ) : Base(a = a, b = b, c = c) fun main() { val derived = Derived(a = "foo", b = "bar", c = "baz") println(derived.concatenation) } This example prints out "nullnullnull", instead of "foobarbaz".
Though if you replace val concatenation = a + b + c with fun concatenation() = a + b + c in the superclass it seems to work just fine with that method call.
Also, IDEA warns of Accessing non-final property <property> in constructor when using val concatenation = a + b + c, but I'm not sure what exactly does this mean.
Is it something with the order of which Base and Derived classes properties initialized? I thought that it might be that I use concatenation from the Base class, but with inheritance I call Base class constructor too and the same properties with same strings must be initialized.
concatenationin the derived class. From my understanding both Base Object and Derived Object constructed should have all data regarding properties a, b and c, because they are directly passed to the both constructors.