If I have an interface in Kotlin:
interface KotlinInterface {
val id: String
}
I can implement it like so:
class MyClass : KotlinInterface {
override val id: String = "id"
}
However, if I were to consume a Java interface like this:
public interface JavaInterface {
String id = "id";
}
I cannot override the id class variable in a similar way:
class MyClass : JavaInterface {
override val myId: String = JavaInterface.id //linter says: 'myId' overrides nothing
}
I also cannot make use of id elsewhere despite it having a predefined value:
class MyClass : JavaInterface {
val myArray: Array<String> = arrayOf(id) // linter would say that id is not defined rather than recognising it as the string “id”
}
It appears I have to use it like so:
class MyClass {
val id: String = JavaInterface.id
val myArray: Array<String> = arrayOf(id)
}
Could anyone explain this difference in behaviour and point out anything I may be understanding wrongly?