invalid access to protected function, weird issue
# announcements
j
invalid access to protected function, weird issue
Copy code
open class Test {
    protected open fun test() {}
}

open class SubClassTest : Test() {
    var classTest: Test = Test()

    public override fun test() {
        //failing because "test()" is protected in Test
        //java is fine with it
        classTest.test()
    }
}
Am I missing something ? this code is perfectly fine in java
d
protected
is more strict in Kotlin than in Java. It does not include "package-private" (since there is no such thing in Kotlin). In Java you can access the
test
here, because its the same package. If
Test
were in a different package than
SubClassTest
this access would not be allowed from Java eitherr.
j
i'm not sure if I follow it... same package, it's marked as protected. Why shouldn't I be able to access it ? java's package-private is not involved in that example, or am I wrong ?
d
Java's protected includes package-private ("default") visibilty.
That is what is granting you access here, not the "protected"-ness.
The definition "subclasses can access it" is a bit misleading. What you can access is that method on instances of the subclass.
j
interesting, thanks for details...