Access protected member of another instance of a superclass

Here’s a smaller example reproducing the issue:

open class Base(protected val value: Int)

class Derived(value: Int): Base(value) {
    fun f1(other: Derived): Derived = Derived(other.value) // OK

    fun f2(other: Base): Base = Base(other.value) // Cannot access 'val value: Int': it is protected in '/Base'.
}

Obviously this behavior is different from Java and it’s not clear from the documentation why it works like this.

There has been some previous discussions about this:
Protected members not visible in subclasses
Cannot access protected method declared in super class

2 Likes