Overriding LiveData

Hi All,
I want to create a cached MutableLiveData class, I can create it in Java, but not in Kotlin. I started to copy MutableLiveData code from Android system & paste in a Kotlin file with Android studio.

open class MutableLiveData<T> : LiveData<T?> {
     constructor(value: T) : super(value) {}
     constructor() : super() {}

    override fun postValue(value: T) {
        super.postValue(value)
    }

    override fun setValue(value: T) {
         super.setValue(value)
    }
}

Compiler said setValue() & postValue() overrides nothing. I saw the original postValue of LiveData and there is no @hide annotation in it:

  protected void postValue(T value) {
    boolean postTask;
    ...
}

How can I solve this problem?
thx
Zamek

You extend with T? but then use T in the override methods. This is the mismatch it’s complaining about. Either switch to LiveData<T> or change your override methods to use parameters of type T?.

Yes, you are right. That was the problem. Thank you very well :slight_smile:

I have the same query