can someone point me on how I can extend a data cl...
# announcements
m
can someone point me on how I can extend a data class? as per the documentation
Since 1.1, data classes may extend other classes
.? I am trying to achieve
Copy code
data class Person(val name: String)
data class Employee(val id: String, val name:String) : Person(name)
k
data classes may extend other classes, but they may not extend other data classes. Data classes are always final
1
m
could I do something like this then?
Copy code
open class Person(val name: String)

data class Employee(val id: String, val name:String) : Person(name)
k
you can't have the
name
property twice
you can do
Copy code
open class Person(open val name: String)
data class Employee(val id: String, override val name:String) : Person(name)
but maybe making
Person
an interface is the better choice
1
m
👍
yeah I have a data object that share most same fields except for extra fields based on certain object.
m
Or sealed class instead of interface when you fancy
when
without
else
.
m
Also I am using it in Android where I have to have a “Parcelable” implementation
m
Writing parcelables by hand?
m
using the IDE plugin to do that. not using libs like paper parcel.
I like how AutoValue generated the parcelable impl
k
why not use PaperParcel there?