Wrt data classes, having inheritance would break the Liskov Substitution Principle wrt equals(), making things break as soon as you use such objects in sets or as map keys.
If all you want is a DTO and don't really care about equals et al (or all the things you get for free with a data class, i.e. destructuring assignment and copy()), then just use a class, not a data class:
Wrt data classes, having inheritance would break the Liskov Substitution Principle wrt
equals()
, making things break as soon as you use such objects in sets or as map keys.There's some discussion about this in errorprone.info/bugpattern/EqualsG...
If all you want is a DTO and don't really care about
equals
et al (or all the things you get for free with adata class
, i.e. destructuring assignment andcopy()
), then just use aclass
, not adata class
:I'd say the biggest problem of data classes is that they're too often abused.
See also jakewharton.com/public-api-challen...