What's a best way to inherit data class?


When I try the following, I get errors:

open data class Person(val name : String, val age : Int)

data class Employee(name : String, age : Int, val role : String) :
  Person(name, age)

fun main(args : Array<String>) {
  println(Person(“test”, 99))
  println(Employee(“test”, 99, “foo”))

output> (3, 1) : Function ‘component1’ generated for the data class conflicts with member of supertype ‘Person’

How should we properly write data class inheritance?



Unfortunately, inheritance of data classes is not supported. We may support them in next versions.

By the way, in Scala case classes (which are similar to data classes in Kotlin) are prohibited.

As a workaround, you can make your base classes abstract class/trait (without ‘data’ word) with abstract properties, and make subclasses data classes.