Using default function implementation of interface in Kotlin

后端 未结 4 1688
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 01:47

I have a Kotlin interface with a default implementation, for instance:

interface Foo {
    fun bar(): String {
        return \"baz\"
    }
}
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 01:58

    Unlike earlier version of Java8, Kotlin can have default implementation in interface.

    When you implement Foo interface into a Java class. Kotlin hides those implementation of interface method. As stated here.

    Arrays are used with primitive datatypes on the Java platform to avoid the cost of boxing/unboxing operations. As Kotlin hides those implementation details, a workaround is required to interface with Java code

    This is specific for Arrays in above link but it also applies to all the classes (May be to give support for earlier version of Java8).

    EDIT

    Above explanation is opinion based.

    One thing i came across and that is the main reason.

    Kotlin binaries were compiled with java bytecode version 1.8 without default methods in interfaces. And they are facing critical issue solving it.

提交回复
热议问题