Devanshu Kaushik
01/29/2022, 8:03 AMA and data class D such that :
abstract class A {
//other fields
open val qty: Int?
}
data class D(
//other fields
override val qty: Int?
): A()
now, when I use class D for auto-serializing my JSON response using gson, I get the following error:
java.lang.IllegalArgumentException:class D declares multiple JSON fields named qty
if I’m overriding qty - won’t it mean that there is actually only one field called qty?Zun
01/29/2022, 10:01 AMDevanshu Kaushik
01/29/2022, 10:19 AMayodele
01/29/2022, 10:20 AMephemient
01/29/2022, 10:51 AMabstract val qty does not create a backing field, only an abstract accessor, so that should help.
as opposed to open val qty creates a backing field and an open accessor, then override val qty creates another backing field (with the same name) and overrides the accessor.
JVM considers these to be two distinct fields, there is no such thing as field overriding, and in fact in your case the field A.qty is not set by D.<init> so it doesn't even have the same valueephemient
01/29/2022, 10:57 AMDevanshu Kaushik
01/29/2022, 6:19 PMopen , thanks for the insight and sure will take a look at Moshi too.ephemient
01/30/2022, 10:12 PMopen must create a non-abstract implementation, which requires a backing field, and that backing field is inaccessible so override val must create another one