https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Colton Idle

03/12/2023, 6:16 PM
So... I thought I knew how interfaces worked... but to my surprise something unexpected is happening. I'm able to compile my code with a new method defined in my interface and I didn't have to override it... can anyone explain?
Copy code
interface MyScreen {
    fun onClickOfSomething(path: String) {}
What's up with that?
c

Chris Lee

03/12/2023, 6:16 PM
the
{}
provides an empty, default implementation of your function.
c

Colton Idle

03/12/2023, 6:28 PM
oh crap. interfaces can have defaults now?
c

Chris Lee

03/12/2023, 6:28 PM
been that way for quite a while, in both Kotlin and Java.
e

ephemient

03/12/2023, 6:30 PM
since Java 8 and before Kotlin 1.0, IIRC
although since Kotlin originally targeted Java 6, the way it works is not entirely compatible (e.g. a Kotlin interface with default implementations may not be easily implemented in Java under some circumstances)
it should "just work" with current versions unless you're doing something unusual, though
c

Colton Idle

03/12/2023, 7:06 PM
interesting. i somehow completely eluded that language feature. TIL. thanks for teaching
a

Almahir Piñango

03/12/2023, 8:54 PM
Ohhh nice to know that .. thanks!
2 Views