Data classes work differently than regular classes. As you have already noticed, data classes require properties in the constructors. Data classes and inheritance is hard, because you can't pass the values like in regular classes. And inheriting from a data class isn't possible either; they don't support open
or abstract
, which means it's final, and can't be inherited from.
Effectively, this isn't possible if you have a data class as the child:
class Something(something: Int, else: Int) : Parent (something, else)
However, Kotlin does support abstract var
s and val
s. If you absolutely need a data class as the child (though I do not recommend that; using a regular class might be better depending on your use case).
abstract class SuperClass {
protected abstract val privateVal: Int
}
data class Overridden(override val privateVal: Int) : SuperClass()
Here it overrides the val in the constructor, meaning it would still work. The reason it's protected
and not private
is because it wouldn't be able to inherit if it's private
.
Again though, I really recommend using regular classes instead of data classes here.