Rajath
Rajath

Reputation: 11926

Using subclasses with RxJava Observable

I'm facing an error with the following code:

interface A1 {
    val string: String
}

data class A2(override var string: String = "") : A1

interface Test {
    fun f(): Observable<List<A1>>
}

fun func(): Observable<List<A2>> = return ...

class TestImpl : Test{
    override fun f(): Observable<List<A1>> = func()
}

There's a type mismatch in the last line of code. How do I change this to have the correct declaration, if at all possible?

Upvotes: 0

Views: 117

Answers (1)

Yoni Gibbs
Yoni Gibbs

Reputation: 7018

You could genericise your interface as follows:

interface Test<T: A1> {
    fun f(): Observable<List<T>>
}

Then implement it as follows:

class TestImpl : Test<A2> {
    override fun f(): Observable<List<A2>> = func()
}

Upvotes: 3

Related Questions