Lets have interface in Java:
public interface Currency {
default String getName() {
throw new NotImplementedException("Implement it!");
}
}
Now I want create an implementation of this interface in Kotlin:
object Dollar : Currency {
val name = "$"
override fun getName() {
return name
}
}
If I initialize Dollar
class by Dollar()
and then call .getName()
on it, I am getting NotImplementedException
exception.
Why is that? I cannot combine Java interfaces with Kotlin implementations?
CodePudding user response:
Your current code
object Dollar : Currency {
val name = "$"
override fun getName(): String {
return name
}
}
Produces this error
Dollar.kt:2:6: error: platform declaration clash: The following declarations have the same JVM signature (getName()Ljava/lang/String;):
fun <get-name>(): String defined in Dollar
fun getName(): String defined in Dollar
val name = "$"
^
Dollar.kt:4:6: error: platform declaration clash: The following declarations have the same JVM signature (getName()Ljava/lang/String;):
fun <get-name>(): String defined in Dollar
fun getName(): String defined in Dollar
override fun getName(): String {
You've actually declared two functions called getName
here as far as the JVM is concerned, you just don't know it yet. Kotlin compiles val
declarations into both an internal (private) field and a getter function, which in your case ends up being called getName
. Now, you can't have a property name
override a Java method getName
, even if they'll end up getting compiled to the same name. Kotlin considers the two distinct and the fact that they end up the same is merely an implementation detail. So simply rename your instance variable and the explicit getter will be the only candidate for override.
object Dollar : Currency {
val _name = "$"
override fun getName(): String {
return _name
}
}
CodePudding user response:
getName(player: Player)
is not the same function as getName()
because it has input arguments and interface does not. Also you are missing retorn type.
If you use
override fun getName() : String{
return name
}
It will work as you expect