# Subclassing a data class, copy method

**URL:** <https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328>\
**Category:** Uncategorized\
**Created:** [February 4, 2015, 2:14pm UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328 "2015-02-04T14:14:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lakedaemon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lakedaemon/32/1421_2.png) [@Lakedaemon](https://discuss.kotlinlang.org/u/Lakedaemon)\
**Post date:** [February 4, 2015, 2:14pm UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/1 "2015-02-04T14:14:18Z")

</div>

Is there a way to do this {code} data open class A(val a:String) data class B(a:String, val b:String):A(a) {code}

When I try this, I get Function ‘component1’ generated for the data class conflicts with members of supertype A

I would like to be able to use the copy method for class B

---

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [February 8, 2015, 1:46pm UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/2 "2015-02-08T13:46:01Z")

</div>

I've run into this too. I don't think there is a workaround at present. The open-ness doesn't propagate to the generated methods.

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [February 23, 2015, 1:21pm UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/3 "2015-02-23T13:21:52Z")

</div>

In Scala it is not allowed to subclass case clases for a good reason. [http://stackoverflow.com/questions/11158929/what-is-so-wrong-with-case-class-inheritance](http://stackoverflow.com/questions/11158929/what-is-so-wrong-with-case-class-inheritance)

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [February 23, 2015, 1:28pm UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/4 "2015-02-23T13:28:43Z")

</div>

This is not enforced by compiler (yet?), but basically data class should be final. There was a recent discussion internally if we want to support abstract data classes, so that data class could be either final or abstract. It allows for some usecases with sharing common data across a number of data classes, but we didn't decide or even investigate it thoroughly.

---

<div class="post-metadata">

**Author:** ![andrewoma](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrewoma](https://discuss.kotlinlang.org/u/andrewoma)\
**Post date:** [February 24, 2015, 5:02am UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/5 "2015-02-24T05:02:19Z")

</div>

Hi there,

Any chance of splitting copy method generation out from data classes? E.g. using a ‘copy’ annotation. I almost always want a copy method but often not equality or to string.

If I want to customise equality for example I have to ditch data classes completely and painfully implement copy manually.

Cheers  
Andrew

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [February 24, 2015, 8:51am UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/6 "2015-02-24T08:51:55Z")

</div>

1) Yes, we are planning to do so 2) You can customize equality in your data classes

---

<div class="post-metadata">

**Author:** ![andrewoma](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrewoma](https://discuss.kotlinlang.org/u/andrewoma)\
**Post date:** [February 24, 2015, 9:37am UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/7 "2015-02-24T09:37:47Z")

</div>

Awesome on both counts!

I swear I tried overriding equals fairly recently and it complained. Anyway, I’m happy to be wrong!

Cheers,  
Andrew

---

<div class="post-metadata">

**Author:** ![Lakedaemon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lakedaemon/32/1421_2.png) [@Lakedaemon](https://discuss.kotlinlang.org/u/Lakedaemon)\
**Post date:** [February 24, 2015, 11:52am UTC](https://discuss.kotlinlang.org/t/subclassing-a-data-class-copy-method/328/8 "2015-02-24T11:52:47Z")

</div>

For 1) Will there be a trait that says that some type has a copy method like {code} trait HasAB : Copy\<T\> { &nbsp;&nbsp;var a:Int &nbsp;&nbsp;var b:String } {code}

I have been playng with RxJava a lot and I often need to make copy of immutable data
