Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
}
like image 598
sharma.mahesh369 Avatar asked Nov 20 '25 15:11

sharma.mahesh369


1 Answers

I got it we need to add

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

}

and mark the concrete method as

@JvmDefault

like image 59
sharma.mahesh369 Avatar answered Nov 23 '25 06:11

sharma.mahesh369