Inheriting class with primary constructor

Viewed 4741

I have a parent class as following,

interface ITask { }

open class Task(val targetServer: Server) : ITask { }

Then there a child inheriting it and overriding the primary constructor as following,

data class FileTask(val sourceServer: Server, targetServer: Server) : Task(targetServer = targetServer) {

}

This is throwing a compilation error in eclipse as

Data class primary constructor must have only property (val / var) parameters

Removing the data keyword from the class header will kill the error, but I don't understand why.

Keeping the data keyword and adding var to the targetServer gives another error

'targetServer' hides member of supertype 'Task' and needs 'override' modifier

Adding override to the targetServer to be override var targetServer: Server throws another error

'targetServer' in 'Task' is final and cannot be overridden

I need some help to understand these errors.

2 Answers

I know this is a very old post, but I was struggling with the same issue and making my superclass abstract was not a solution. You just need to do the following:

change this

open class Task(val targetServer: Server) : ITask { }

to (Please note, I have made targetServer variable to open)

open class Task(open val targetServer: Server) : ITask { }
Related