https://kotlinlang.org logo
#android
Title
# android
s

Sam

09/16/2019, 9:29 AM
Hello everybody, I have this
Listener
, but I only want to implement one or two methods, not all of methods. So what should I need to change ? Thanks
e

Efe

09/16/2019, 9:31 AM
it is the definition of interface. If you dont want to implement all of them you need more than 1 interface
m

Marek Osvald

09/16/2019, 9:31 AM
Hi, @Sam. Add a default implementation to optional methods.
Copy code
interface Listener {
    fun onSubmitted(text: String) // mandatory
    fun onClickedRightIcon() { } // optional
}
🎉 4
s

Sam

09/16/2019, 9:41 AM
Thanks @Efe @Marek Osvald
@Marek Osvald: What’s name of this syntax
fun onClickedRightIcon() { }
?
e

Efe

09/16/2019, 9:42 AM
default implementation 🤔
K 2
🎉 1
✔️ 1
d

dewildte

09/16/2019, 12:37 PM
Default implementation in Kotlin can have problems interacting with Java. You can also use the Adapter Pattern, a open or abstract class that implements both functions but they do nothing. Your concrete class will implement the interface using
by
delegation of your adapter, but only override the functions it needs.
K 2
s

Sam

09/16/2019, 12:53 PM
@dewildte: `Your concrete class will implement the interface using
by
delegation of your adapter, but only override the functions it needs. (edited) ` => can you give me some example about that pattern ?
r

rkeazor

09/16/2019, 1:01 PM
I second the Adapter pattern with abstract class