I've got a question concerning inheriting from an ...
# language-proposals
s
I've got a question concerning inheriting from an abstract class on a data class. Let say we have the following abstract class:
Copy code
abstract class AbstractExample(
    open val paramOne: Int,
    open val paramTwo: String
)
And, the following implementation of the example abstract class:
Copy code
data class ExampleImpl(
        override val paramOne: Int,
        override val paramTwo: String,
        val paramThree: Boolean
) : AbstractExample(paramOne, paramTwo)
I have to override
paramOne
and
paramTwo
, because of the following message I get: 'Data class primary constructor must have only property (val / var) parameters' I'd ideally however have an
ExampeImpl
like so:
Copy code
data class ExampleImpl(
        paramOne: Int,
        paramTwo: String,
        val paramThree: Boolean
) : AbstractExample(paramOne, paramTwo)
Or this at least looks cleaner to me, since this doesn't mean I need to instantiate
paramOne
and
paramTwo
for
ExampleImpl
again and thus use the variables and getter/setter from the
AbstractExample
as I'd prefer to. As far as I know this currently isn't a possibility, hence I dropped in this channel.
k
smcvb: note that in your example, both the super and the child class have fields for the two overriden properties but only the child class' ones are use. instead, you should make the properties in the base class `abstract`:
Copy code
abstract class Base {
    abstract val paramOne: Int
    abstract val paramTwo: String
}
s
Ow awesome how could I have missed that!
Thanks a bunch! 🙂