Why cannot protected members be exposed as public members in Kotlin?


#1

In Java, we cannot reduce the visibility or Scope of member/function. A public member cannot be made as private in the subclass, but a protected member can be made as public. Why is it not allowed in Kotlin?


#2

It is in fact allowed in Kotlin. This code is perfectly valid:

open class A {
    protected open fun foo() {}
}

class B : A() {
    override public fun foo() {}
}

#3

Ah I see. I got carried away by this statement.

If you override a protected member and do not specify the visibility explicitly, the overriding member will also have protected visibility.

But it does not stop us to expose it as public member.


#4

I was enjoying the features of protected modifier in java in which protected is visible within the package. All of sudden, Kotlin changed the scope and its bit hard to see the change of scope that protected modifier is private and visible only to subclasses (subclass private, i call it).