Can someone explain why in the following code, I c...
# announcements
j
Can someone explain why in the following code, I can override a
val
property with a subtype of the original type without problem, but trying the same with a
var
property fails?
Copy code
open class Parent {

    open val valProperty : Parent
        get() = Parent()

    open var varProperty: Parent
        get() = Parent()
        set(value) {
            // doing something
        }
}

class Child : Parent() {

    override val valProperty : Child
        get() = Child()

    override var varProperty: Child
        get() = Child()
        set(value) {
            // doing something
        }
}
IntelliJ error message is:
Var-property type is Child which is not a type of overriden public open var varProperty: Parent defined in ...