I have a Kotlin interface with a default implementation, for example:
interface Foo { fun bar(): String { return "baz" } }
This will be fine until I try to implement this interface with Java. When I do this, he says that the class should be marked as abstract or implement the method bar(). Also, when I try to implement a method, I cannot call super.bar().
bar()
super.bar()
Please see the related issue .
There is a recommendation in the comments:
Write your interface in Java (with default methods), and the Java and Kotlin classes use these default values correctly
default Java, Kotlin 1.2.40.
default
@JvmDefault @JvmDefault:
@JvmDefault
interface Foo { @JvmDefault fun bar(): String { return "baz" } }
- , -Xjvm-default=enable . ( Gradle, ).
-Xjvm-default=enable
, . , , , , , IDE Java - - , , , .
Java8, Kotlin .
Foo Java. Kotlin . .
Foo
Java, /. Kotlin , Java-.
, ( , Java8).
.
, , .
Kotlin binaries were compiled using java-bytecode version 1.8 without standard methods in the interfaces. And they are faced with a decisive problem that solves it.
Source: https://habr.com/ru/post/1677910/More articles:Regex replacement takes time for millions of documents, how to do it faster? - pythonWhy does the eslint report resolve the error: cannot resolve resolver "webpack"? - webpackDrawIo mxGraph: using XmlToSvg loses shape information - mxgraphDifference between anyMatch and findAny in java 8 - javaIs Stream.findAny short circuit operation? - javaHTML5 input date format with angular scope - javascriptRunning an EMR job from an ECS Docker container - amazon-emrCan Spring boot test classes reuse the application context to run the test faster? - javaSqlite on Azure: сервер не найден или недоступен - sqliteHeadless / Unleashed CMS Offers - content-management-systemAll Articles