There is no way to resolve this in Kotlin, and here is why:
The difference is that protected
actually means something subtly different in Kotlin than in Java.
protected
in Kotlin means:
kotlin protected: same as private (visible inside the file containing the declaration) + visible in subclasses too;
protected
in Java means:
java protected: the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
And with this knowledge the issue should be clear, the protected static class B
in Kotlin is more like private static class B
in Java. Therefore the warning is correct.
The Kotlin-Java Interop guide specifically states:
protected
remains protected
(note that Java allows accessing protected members from other classes in the same package and Kotlin doesn't, so Java classes will have broader access to the code);
Conclusion:
This means that Kotlin interprets the Java-protected
as if it was a Kotlin-protected
ergo there is no way to implement the class K
in Kotlin as it is. The least you must do to make it work is create C extends A
(in Java) that handles all public access of B
and then extend this class in Kotlin. Like in this issue Calling protected static methods
The culprit:
The main problem is Javas behaviour of static nested classes, which
interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
This convenient behaviour creates the problem in the first place.
Side note:
Probably the better match for Java-protected
is Kotlins internal
which provides a better level of encapsulation.
kotlin internal: any client inside this module who sees the declaring class sees its internal members;