1

I have an interface MyInterface in Kotlin with some methods that some of them have a default implementation. Also, I have another interface IClass that extends MyInterface written in Java. At last, I have a class MyClass that implements MyInteface written in Kotlin. Now when I run my code, I got the following error.

java.lang.ClassNotFoundException: myPackage.IClass$DefaultImpls

I test both Kotlin 1.2.51 and 1.3.0

When I write IClass in Kotlin, everything works as expected. What's wrong with my first code?

Hossein Nasr
  • 1,436
  • 2
  • 20
  • 40

2 Answers2

3

This is a bug in the Kotlin compiler. As a workaround, please do use @JvmDefault as recommended in another answer.

yole
  • 92,896
  • 20
  • 260
  • 197
  • As of Kotlin 1.4, you can now set the `-Xjvm-default=all` compiler option. See https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#default-methods-in-interfaces and https://blog.jetbrains.com/kotlin/2020/07/kotlin-1-4-m3-generating-default-methods-in-interfaces/ for more details. – ashughes Oct 26 '20 at 19:27
2

You probably want to annotate your method with JvmDefault in Kotlin. It's a new feature described in detail here

RedDeckWins
  • 2,111
  • 14
  • 16
  • As of Kotlin 1.4, you can now set the `-Xjvm-default=all` compiler option. See https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#default-methods-in-interfaces and https://blog.jetbrains.com/kotlin/2020/07/kotlin-1-4-m3-generating-default-methods-in-interfaces/ for more details. – ashughes Oct 26 '20 at 19:26