https://kotlinlang.org logo
#announcements
Title
# announcements
m

manijshrestha

04/24/2017, 8:07 PM
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

kevinmost

04/24/2017, 8:08 PM
data classes may extend other classes, but they may not extend other data classes. Data classes are always final
1
m

manijshrestha

04/24/2017, 8:09 PM
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

kirillrakhman

04/24/2017, 8:10 PM
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

manijshrestha

04/24/2017, 8:12 PM
👍
yeah I have a data object that share most same fields except for extra fields based on certain object.
m

mg6maciej

04/24/2017, 8:13 PM
Or sealed class instead of interface when you fancy
when
without
else
.
m

manijshrestha

04/24/2017, 8:13 PM
Also I am using it in Android where I have to have a “Parcelable” implementation
m

mg6maciej

04/24/2017, 8:20 PM
Writing parcelables by hand?
m

manijshrestha

04/24/2017, 8:33 PM
using the IDE plugin to do that. not using libs like paper parcel.
I like how AutoValue generated the parcelable impl
k

kevinmost

04/24/2017, 8:39 PM
why not use PaperParcel there?
5 Views