https://kotlinlang.org logo
Title
a

Arnau Díaz

10/22/2019, 8:13 AM
Hi guys, I'm having trouble understanding the difference between these: With abstract class:
abstract class ParentObject(val id: Int)
class ChildObject(id: Int, val value: String) : ParentObject(id)
With adt/sealed class it doesn't compile:
sealed class ParentObject(val id: Int)
data class ChildObject(id: Int, val value: String) : ParentObject(id)
Is there any way to get it working in Kotlin? I want the benefits of the data classes but there are some fields that are common for all my subtypes.
s

spand

10/22/2019, 8:15 AM
sealed class ParentObject {
    abstract val id: Int
}
data class ChildObject(override val id: Int, val value: String) : ParentObject()
👍 1
a

Arnau Díaz

10/22/2019, 8:18 AM
Thank you for your quick reply- I was trying to follow the compiler error and use the "overriding" somewhere 🙂 but I couldn't get it to work.