As the documentation states,
- Data classes cannot be abstract, open, sealed or inner;
The reason why they cannot be inherited is that inheriting from a data class causes an ambiguity with how their generated methods (equals
, hashcode
, etc.) should work. See further discussion about this in an answer to another question.
Since Kotlin 1.1, the restrictions on data classes have been lifted slightly: They can now inherit from other classes, as described in detail in the related proposal. However, they still cannot be inherited from themselves.
Note that data classes “only” provide the extra convenience of the automatic equals
, hashcode
, toString
, component
, and copy
functions. If you don’t need those, then a class like the following still has properties with getters/setters and a constructor in a very brief form, and has no limitations on how you can use it with inheritance:
class User(val name: String, var age: Int)