Hi! Does anyone know if it’s possible to have an a...
# android
c
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
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
Awesome, thanks 🙂