Dumb question. I declare a property in an abstract...
# announcements
s
Dumb question. I declare a property in an abstract class as
protected var counter: Int
, which is naturally visible in the implementation classes, but not outside. If I try to access protected member from "outside", I get
Can not access 'counter': it is protected in ...
compilation error. This is in Kotlin-only code. However from Java I can see all the protected members as well and can access
getCounter()
and
setCounter()
. Seems like a strange behavior to me and I couldn't fine anything in the docs.
l
Are you subclassing from Java?
Or is the Java code in the same package as the Kotlin superclass?
Because in Java,
protected
is also implictly
package-private
(i.e. not so protected)
s
1. No. 2. yes 🤦‍♂️
Thanks 🙂
l
BTW, if you want to guard from Java callers using the same package name, you can try the
@JvmSynthetic
annotation.
👍 1
k
But then you don't allow Java subclasses either.
l
You don't allow access to this field/property/method from Java, yes.