After knowing Kotlin, love the data class
.
I could replace Java classes that has equal
and hash
and toString
to it.
Most of these Java classes are serializable
class. So my question is, when we convert to data class
, do I still need to make it serializable
explicitly? like
data class SomeJavaToKotlinClass(val member: String) : Serializable
Or it is okay to be
data class SomeJavaToKotlinClass(val member: String)
No, Kotlin data classes do not implicitly implement this interface. You can see from this example:
import java.io.Serializable
data class Foo(val bar: String)
fun acceptsSerializable(s: Serializable) { }
fun main(args: Array<String>) {
val f: Foo = Foo("baz")
// The following line produces the compiler error:
// Type mismatch: inferred type is Foo but Serializable was expected
acceptsSerializable(f)
}