Extending Kotlin class by Java requires me to reimplement already implemented method

Looking at the byte code we can see, that the Impl-class basically has produced the following function:

public go(I)V

where the parameter is a primitive integer. Also a synthetic bridge-function (go(Object)) is generated, which however would also be generated on the Java-side for such generic functions.

On the Java side however it doesn't suffice to have something like public void go(int field) in place. Now we need that go(Integer field)-function, which isn't present. For me that sound's like an interop-problem that should probably be reported and linked back here again. Actually having had some time to investigate, there seem to be some issues already: KT-17159 and KT-30419, but also KT-5128 seem to relate to this problem. The kotlin compiler knows how to deal with this and doesn't need any further information about it in the class-file (i.e. the implementing Kotlin class knows, that it doesn't need to implement something like fun go(field : Int?)). For the Java-side such counterpart does not exist. I wonder whether this could even be fixed nicely with the compiler/byte-code or whether this will remain a specific interop-problem.

Some workarounds to deal with that problem (in case this is deliberate and not a real problem):

  1. Add an additional function as follows to Impl:

    fun go(field : Int?) = go(field ?: error("Actually the given field should never be null"))
    // or simply:
    fun go(field : Int?) = go(field!!)
    

    That way you would not need to implement it. However then you would also expose that nullable function to the Kotlin side, which you probably don't want.

  2. For that specific purpose it may seem more convenient to do it the other way around: declare the class and the interface in Java and use it on the Kotlin side. That way you could still declare something like

    abstract class KotlinClass : JavaInterface<Int> {
      override fun go(field : Int) { // an IDE might suggest you to use Int? here...
        // ...
      }
    }
    

You can use javap to analyze the problem, showing members of compiled interface and classes.

javap Base

public interface Base<T> {
  public abstract void go(T);
}

javap Impl

public abstract class Impl implements Base<java.lang.Integer> {
  public void go(int);
  public void go(java.lang.Object);
  public Impl();
}

So, the problem is exactly that pointed out by @Roland: in order to satisfay the contract requested by the Base interface, the Java compiler needs a public void go(java.lang.Integer) method but the method generated by Kotlin compiler has int as parameter.

If you implement the interface in Java, with something like

class JI implements Base<Integer> {
    @Override
    public void go(@NotNull Integer field) {

    }
}

you can analyze its compiled version with javap obtaining

javap JI

class JI implements Base<java.lang.Integer> {
  org.amicofragile.learning.kt.JI();
  public void go(java.lang.Integer);
  public void go(java.lang.Object);
}

So, if you plan to use Kotlin class Impl as superclass of Java classes, the solution is simply to use <Integer>, not <Int>, as type parameter: Int is a Kotlin class, translated to int by the compiler; Integer is the Java class you usually use in Java code.

Changing your example to

abstract class Impl : Base<Integer> {
    override fun go(field: Integer) {}
}

public class JavaImpl extends Impl {
}

the JavaImpl Java class compiles without errors.

Tags:

Java

Kotlin