LastExceed
05/15/2021, 9:49 AMfoo
being printed here even though x is overriden ?
edit: oops wrong channeldiesieben07
05/15/2021, 9:52 AMvar
).
In your case both A.x
and B.x
have a backing field which is initialized. The initializer is not something that is overridden and neither is the backing field. Both exist in both A
and B
.LastExceed
05/15/2021, 9:53 AMsuper.x
which still stores 1LastExceed
05/15/2021, 9:53 AMLastExceed
05/15/2021, 9:54 AMuli
05/15/2021, 10:30 PMThe initializer is not something that is overridden and neither is the backing field.
Meaning there is only one common x
for A
and B
. But this x
is initialized twice, leaving it with it's last value (2). But keeping the side effects from all initializersephemient
05/16/2021, 1:11 AMsuper.x
.ephemient
05/16/2021, 1:14 AMval x = 1
does multiple things:
- declare a private backing field
- declare a public getter
- adds initialization to the (primary) constructor
an override val
can only override the getter, none of the others.