0

I have a data class in Kotlin that inherits from a Java class, which defines a constructor with 1 argument,

public BaseClass(String userSessionId) {
    this.userSessionId = userSessionId;
}

My Kotlin class is defined as this

class DerivedClass(
    userSessionId: String,
    var other: Other? = null
) : BaseClass(userSessionId) {

I can't define it as a data class because of userSessionId, which Kotlin requires to be a val or var in data classes. However, if I do so, then Retrofit throws an exception because there are 2 members named userSessionId. Is there a way to have a data class inherit from a Java class with a constructor taking arguments? Note that I cannot change the base class.

A possible solution is to define a dummy val to avoid the name clash, but this is less than ideal

data class DerivedClass(
    val dummy: String,
    var other: Other? = null
) : BaseClass(dummy) {
3
  • 1
    How about marking the property in DerivedClass as @Transient? Retrofit should ignore it as per this answer. Commented Nov 30, 2017 at 20:17
  • Define it as a val or var and use @SerializedName("userSessionId") before the field in the data class. This should do the trick. Commented Nov 30, 2017 at 21:12
  • @zsmb13 Transient works. Please create an answer and I'll accept it. Commented Nov 30, 2017 at 21:28

1 Answer 1

2

You can use the transient keyword in Java to ignore a field during serialization, this can be done in Kotlin by using the @Transient annotation on the property instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.