Inheritance for nullable properties

I want to override nullable properties in child class

open class IssueBase {
    open var id: Long? = null
    open var subject: String? = null
    open var priority: Priority? = null
}

class Issue : IssueBase() {
    override var id: Long            //This redeclaration works fine!!!
    override var subject: String     //Compile time Error
    override var priority: Priority  //Compile time Error

    constructor(id: Long, subject: String, priority: Priority){
        this.id=id
        this.subject = subject
        this.priority = priority
    }
}

Priority class is simply class with 2 fields:
class Priority(val id: Long, val name: String)

And I get the error:
var-property type is ‘String’, which is not a type of overridden public open var subject: String? defined in packagename.IssueBase
and
var-property type is ‘Priority’, which is not a type of overridden public open var priority: Priority? defined in packagename.IssueBase

but property id redeclared fine:
override var id: Long - without compile time error

For which reasons I want this redeclaration of nullable types?
The purpose of the class IssueBase is for send data to server(ex POST)
The purpose of the class Issue is hold data from the server (ex GET)

All of these overrides are highlighted as errors for me. The reason is that the base class interface allows to set the property to null, and this would break the requirements of the derived class.

Such an override will work if you declare the properties of IssueBase as val and create two derived classes, one with nullable and one with non-null properties.

1 Like