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

christian.steffensen

03/14/2019, 8:52 AM
Hi! Does anyone know if it’s possible to have an abstract function that takes an interface as parameter, and then when overriding that method it would use the specific implementation of that interface? I’m guessing I have to use generics, but I can’t get it to work…
Copy code
class StartViewModel() : BaseViewModel() {
    sealed class Input : IInput {
        object ButtonClicked : Input()
    }

    override fun handleInput(input: IInput) { //I want this to be of the type Input, instead of the interface      
    }
}

abstract class BaseViewModel {
    interface IInput
    protected abstract fun handleInput(input: IInput)

    init {
        handleInput(something that implements IInput)       
    }
}
s

skoric

03/14/2019, 9:18 AM
Copy code
class StartViewModel() : BaseViewModel<StartViewModel.Input>() {
    sealed class Input : IInput {
        object ButtonClicked: Input()
    }

    override fun handleInput(input: Input) {

    }
}

abstract class BaseViewModel<T : BaseViewModel.IInput> {
    interface IInput
    protected abstract fun handleInput(input: T)
}
c

christian.steffensen

03/14/2019, 10:03 AM
Awesome, thanks 🙂