Junior — Middle
How is the extension interface applied in code?
sobes.tech AI
Answer from AI
Extension interface in Android usually refers to the use of extensions in Kotlin, which allow adding new functions to existing classes without inheritance.
The application of extension interface looks like this:
interface Clickable {
fun click()
}
// Extension for the Clickable interface
fun Clickable.showClick() {
println("Clicked!")
}
class Button : Clickable {
override fun click() {
println("Button clicked")
}
}
fun main() {
val button = Button()
button.click() // Calling the interface method
button.showClick() // Calling the extension
}
Thus, extensions allow adding new methods to interfaces or classes without changing their original code, which improves code readability and extensibility.