简体   繁体   中英

Why concrete method in interface must be implemented if the interface is written in kotlin

I have written an interface in kotlin with a concrete method and the class which implements it is asking about overriding it. If I write the same interface in java it works fine. May I know what's wrong with it?

In kotlin

interface BottomSheet {

companion object{

    val BOTTOMSHEET_ADAPTER:String="BOTTOMSHEET_ADAPTER"
    val FILTER_ADAPTER:String="FILTER_ADAPTER"

}

fun getBottomSheetAdapterInstance(context: Context?): BottomSheetAdapter? {
    return BottomSheetAdapter(context)
}

}

in Java

public interface BottomSheetJava {

default BottomSheetAdapter getBottomSheetAdapterInstance(Context context){
    return new BottomSheetAdapter(context);
}

}

in gradle

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}

I got it we need to add

kotlinOptions { jvmTarget = "1.8" freeCompilerArgs = ['-Xjvm-default=enable']

}

and mark the concrete method as

@JvmDefault

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM