Inheriting a Java class using a public method that accepts a protected class in Kotlin

I have this situation: There is a Java class

public class A { public void overrideMe(B param){ //TODO: override me in Kotlin! } protected static class B { } } 

and the Kotlin class, which inherits from it and must override the "overrideMe" method

 class K: A() { override fun overrideMe(param: B) { println("Wow!") } } 

But Kotlin does not allow such behavior.

'public' function provides its parameter 'protected (in A) of type B

Is there any way to solve this problem?

PS This is not just a synthetic case - I ran into this problem when I tried to implement a custom Spring AmqpAppender and override its postProcessMessageBeforeSend method.

+7
java inheritance protected kotlin kotlin-interop
source share
2 answers

Well, in the end, the conclusion is this: there is no way to solve this situation in pure Kotlin.

I hope AmqpAppender.Event will be publicly available in the near future.

Even if Java allows this behavior, the lack of public arguments in public methods seems like a bad design to me (also for Kotlin developers).

0
source share

Kotlin has no way to resolve this , and here's why:

The difference is that protected actually means something very subtle in Kotlin than in Java.

protected in Kotlin means:

kotlin protected: the same as private (visible inside the file containing the declaration) +, also visible in subclasses;

protected in Java means:

protected java : access to an element can only be accessed in its own package (as well as in the private package) and, in addition, by subclassing its class in another package.

And with this knowledge, the problem should be clear, protected static class B in Kotlin is more like private static class B in Java. Therefore, the warning is correct.

The Kotlin-Java Interop manual states the following:

protected remains protected (note that Java allows access to protected members from other classes in one package, and Kotlin does not, so Java classes will have wider access to the code );

Output:

This means that Kotlin interprets Java-protected as if it were Kotlin- protected ergo, there is no way to implement class K in Kotlin as it is. The least you need to do to get it working is to create C extends A (in Java) that handles all of B public access and then extends this class in Kotlin. As in this release, Calling Protected Static Methods

Criminal:

The main problem is the behavior of Javas static nested classes , which

interacts with instance members of its outer class (and other classes), like any other top-level class. In essence, a static nested class is behaviorally a top-level class that was nested in another top-level class for ease of packaging .

This convenient behavior creates the problem first.

Side note:

Probably the best match for Java- protected is Kotlins internal , which provides the best level of encapsulation.

kotlin internal: any client inside this module that sees the declaration class sees its internal members,

+1
source share

All Articles