0

I want to create data class extend from another data class. I can not extend from another data class because of another data class is data class and i have no chance that change another data class. Another data class must be data class (not interface, open class etc.).

I searched on internet and everyone says you should remove data tag and write open class. So how to extends from data class ?

feyfhe
  • 3
  • 1

1 Answers1

3

You cannot extend a data class with another. However, you can create an object of that class like below:

data class Student(
    val rollNo: Int,
    val person: Person
)

Parent data class

data class Person(
    val fullName: String
)

Usage

val student = Student(15, Person("Developer"))
Log.d("MyTag", student.person.fullName)
Log.d("MyTag", student.rollNo)
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23