I have two modules defines like this:
interface Module {
val parameter: JSONObject
}
abstract class FirstModule: Module {
abstract val message: Message
}
....
// many more Modules
I created a base listener interface for Module and another for FirstModule:
fun interface ModuleListener {
fun ready(module: Module)
}
fun interface FirstModuleListener: ModuleListener {
fun ready(module : FirstModule)
override fun ready(module: Module) {
ready(module as FirstModule)
}
}
assuming there is a function named firstModule() like this:
fun firstModule(listener: FirstModuleListener)
In KOTLIN when I call firstModule() things work as expected (to let user only override ready() with FirstModule as parameter:
firstModule ( object: FirstModuleListener {
override fun ready(module: FirstModule) {
Log.v(TAG, "module: $module)
}
}
}
but the same is behaving differently in JAVA. It is expecting user to override both the interface methods even when one of them is already overridden and has some content in child interface.
firstModule(new FirstModuleListener() {
@Override public void ready(Module module) { }
@Override public void ready(FirstModule module) {}
}
How can I skip the first overridden method call in Java. Is it causing due to the method definition support in Kotlin and not in Java?
Essentially: When
kotlinimplementsFirstModuleListenerinterface, it still implements twoready().In
FirstModuleListener,fun ready(module: FirstModule)is an abstract method, andIt is a concrete realization method.
The interface in kotlin can define specific methods, and the kotlin compiler behind it is converted into the corresponding static method in Java. Your
FirstModuleListenerdefines both abstract and non-abstract methods.Kotlin will generate a
DefaultImplsstatic inner class to implement theready(module: Module)method ofFirstModuleListenerat compile time.When implementing
FirstModuleListener, a ready() essentially callsready()ofDefaultImpls.This is no different from Java.
#################################################