``` open class BaseViewModel { private val _ev...
# android
k
Copy code
open class BaseViewModel {
    private val _event = SingleLiveEvent<Events>()
    val event: LiveData<Events> = _event

    sealed class Events {
        data class ShowMessage(val message: String) : Events()
    }
}

class LoginViewModel : BaseViewModel() {

    object LoginSuccess : Events()
    //Cannot access '<init>': it is private in 'Events'.
    // This type is sealed, so it can be inherited by only its own nested classes or objects.
}
is there a way to achieve it somehow ? i'm getting ->
Cannot access '<init>': it is private in 'Events'.This type is sealed, so it can be inherited by only its own nested classes or objects.
j
Use
protected
instead of
private
j
@Javier a sealed constructor is always private if I'm not wrong, so that will not work. @Kulwinder Singh I think the error tells you what to do: put the events next to each other in the same file/class, because now they are in nested classes. Also you can re-evaluate if you really need a sealed for this, would an interface not suffice?
j
Ah, I didn't read the problem correctly. I thought he can't access to the mutableLiveData
You can't extend a sealed class outside the same file. I think it will be possible in future kotlin version.
e
Copy code
open class BaseViewModel {
    private val _event = SingleLiveEvent<Events>()
    val event: LiveData<Events> = _event
    sealed class Events {
        data class ShowMessage(val message: String) : Events()
        object LoginSuccess : Events()
    }
}
class LoginViewModel : BaseViewModel() {
   val loginSuccess = Events.LoginSuccess
// can be either showMessage or loginSuccess 
   lateinit var eventType: Events
}