I have an issue with `protected` visibility on a p...
# getting-started
n
I have an issue with
protected
visibility on a parent in a sealed hierarchy:
Copy code
sealed class Test {
    protected fun test() = Unit
}

class SubTest : Test() {
    private val tests = mutableListOf<Test>()

    private fun accessTestFunction() {
        tests.forEach(Test::test)
    }
}
In here the
Test::test
inside of
accessTestFunction
is not visible. I'd except it to be visible because
SubTest
is a subclass of
Test
, and indeed it is visible on the
this
scope, but not for other instances of
Test
. This code would work perfectly fine in Java, however for some reason it doesn't work in Kotlin. In this an intentional limitation, or a compiler bug?
h
Seems like the behaviour you expect was considered a bug: https://youtrack.jetbrains.com/issue/KT-11379/access-to-protected-should-not-be-allowed. But it still seems a bit odd, considering the following for example:
Copy code
open class A {
    protected val foo = 1
}

class B : A() {
    fun bar(a : A) = a.foo // Cannot access 'fun foo(): Unit': it is protected in 'A'.
    fun bar(b : B) = b.foo // OK
}