Pere Casafont
12/09/2018, 10:51 AMopen class ParentFoo
class ChildFoo : ParentFoo()
abstract class ParentBar {
open var foo: ParentFoo? = null
}
class ChildBar : ParentBar() {
override var foo: ChildFoo?
get() = super.foo as ChildFoo?
set(value) {
super.foo = value
}
}
right now I'm not allowed to set ChildFoo as the `ChildBar`'s foo property type as it is not exactly the same type as its parent's foo, but as it doesn't have a backing field I think this should be able to compile without any problem. What am I missing there?Pere Casafont
12/09/2018, 11:04 AMPere Casafont
12/09/2018, 11:08 AMPavlo Liapota
12/09/2018, 12:17 PMChildBar should contain only ChildFoo in foo, then I would have an abstract val foo in `ParentBar`:
open class ParentFoo
class ChildFoo : ParentFoo()
abstract class ParentBar {
abstract val foo: ParentFoo?
}
class ChildBar : ParentBar() {
override var foo: ChildFoo? = null
}
That way you will be able to get ParentFoo from ParentBar and get and set ChildFoo from ChildBar.Pavlo Liapota
12/09/2018, 12:25 PMopen class ParentFoo
class ChildFoo : ParentFoo()
abstract class ParentBar<TFoo : ParentFoo> {
var foo: TFoo? = null
}
class ChildBar : ParentBar<ChildFoo>() {
}Pere Casafont
12/20/2018, 10:21 AM