Reputation: 33
I have a really simple question here, but can't figure it out. Consider a java class:
class A {
public static final int AA=5;
}
which translates (internally) into kotlin as this:
open class A {
companion object {
val AA:Int=5
}
}
At least that's what I assume. Now if you inherit in java:
class B extends A {
int AAA;
}
you can access field AA (from A) through B like this: B.AA . However, in Kotlin it's impossible. The only way to access it is through A.AA. Is this a built-in feature or I am doing something wrong?
Upvotes: 2
Views: 540
Reputation: 170745
In Java, when the compiler sees B.AA
, it automatically transforms it into A.AA
. There's no real inheritance or overriding there.
Kotlin's developers decided not to emulate this feature, because it doesn't fit with thinking of static methods as belonging to an object
: the companion object of B
doesn't extend the companion object of A
, and can't because you can't extend an object
.
Note that similarly, in Java you can access the field via a.AA
where a
is an instance of A
; you can't do that in Kotlin either.
Upvotes: 3