I have this utility interface to be implemented by RecyclerView's ViewHolder that fires UI events.
interface ObservableMvpViewHolder<V> {
val listeners: List<V>
fun registerListener(listener:V)
fun unregisterListener (listener: V)
}
the listeners
property is like a contract so I want to obligate the clients to declare it to store the observers.
but when I implement this interface I have to declare a getter to this property:
class AddItemViewHolderHolder(override val containerView: View) : ViewHolder(containerView), LayoutContainer, ObservableMvpViewHolder<AddItemViewHolderHolder.Listener> {
override val listeners: List<Listener>
get() = listeners
I don't want to do that to avoid to expose this property to the outside.
I'm new to Kotlin, is there a way to do that without having to declare an abstract class?
Interface as you've said are Contract that you let everyone read, therefore, it doesn't make sense to hide a property that is part of that contract.
Interface Documentation
So as you've said, you could achieve this behavior using an abstract class :