# Data class extend sealed class

**URL:** <https://discuss.kotlinlang.org/t/data-class-extend-sealed-class/6883>\
**Category:** Language Design\
**Created:** [March 5, 2018, 11:13am UTC](https://discuss.kotlinlang.org/t/data-class-extend-sealed-class/6883 "2018-03-05T11:13:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [March 5, 2018, 11:13am UTC](https://discuss.kotlinlang.org/t/data-class-extend-sealed-class/6883/1 "2018-03-05T11:13:09Z")

</div>

I recently face problems of extending sealed class in data class . The same with this SO link

> <https://stackoverflow.com/questions/44419997/extending-data-class-from-a-sealed-class-in-kotlin>

The solution is OK :

> ```
> sealed class Message {
> abstract val messageId: String
> }
> data class Track(val event: String, override val messageId: String): Message()
> 
> ```

But I wonder why Option2 is not supported ?

> ```
> sealed class Message(val messageId: String)
> data class Track(val event: String, messageId: String): Message(messageId)
> 
> ```

It seems intuitional (and maybe more ideal) , but pity it’s not supported.

Yes , I know the reason :

> (“Data class primary constructor must have only property (val/var) parameters.”)

But I think it should be able to infer it as a superclass’s val parameter and passes.

Just my 2 cents.

---

<div class="post-metadata">

**Author:** ![devtripart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/devtripart/32/4833_2.png) [@devtripart](https://discuss.kotlinlang.org/u/devtripart)\
**Post date:** [March 5, 2018, 4:20pm UTC](https://discuss.kotlinlang.org/t/data-class-extend-sealed-class/6883/2 "2018-03-05T16:20:31Z")

</div>

It’s compatible.  
According to the references:

To declare an explicit supertype, we place the type after a colon in the class header:  
If the derived class has a primary constructor, the base class can (and must) be initialized right there, using the parameters of the primary constructor.

**[https://kotlinlang.org/docs/reference/classes.html](https://kotlinlang.org/docs/reference/classes.html)**

In the code, the class was declared as open and the value of the subclass with override.

**open:**

The open annotation on a class is the opposite of Java’s final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to Effective Java, Item 17: Design and document for inheritance or else prohibit it.

**override:**

Annotation override signals the overriding of base method by inheriting class.

The code would be like this:

```
sealed class Message(open val messageId: String)
data class Track(val event: String, override val messageId: String) : Message(messageId)

```

---

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [March 5, 2018, 5:19pm UTC](https://discuss.kotlinlang.org/t/data-class-extend-sealed-class/6883/3 "2018-03-05T17:19:39Z")

</div>

Wow , thanks for great help !
