Why Kotlin modifier 'open' is incompatible with 'data'?

From https://kotlinlang.org/docs/reference/data-classes.html:

To ensure consistency and meaningful behavior of the generated code, data classes have to fulfil the following requirements:

  • The primary constructor needs to have at least one parameter;
  • All primary constructor parameters need to be marked as val or var;
  • Data classes cannot be abstract, open, sealed or inner;
  • (before 1.1) Data classes may only implement interfaces.

So the main point is that data class has some generated code (equals, hashCode, copy, toString, componentN functions). And such code must not be broken by the programmer. As a result, data class has some restrictions.


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)