Because the Java language required all interface members to be public and the original designers didn't want to force the "methods" of java.io.Serializable
to be public, this was not possible in Java.
Scala doesn't have this restriction, but things like readObject
/writeObject
are still not specified in the scala.Serializable
trait.
Wouldn't this help developers because
or do I miss something important?
Serializable doesn't contain any method, it's the ObjectOutputStream and ObjectInputStream classes that can do that work, through the writeObject and readObject methods. Serializable is just a marker interface, in other words it just puts a flag, without requiring any fields or methods.
Serializing an object means taking the data stored in an object and converting it to bytes (or a string). Suppose you want to write the data in an object to a JSON file. JSON files store strings. JSON has no understanding about the JVM or Scala objects.
The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods. Classes implement it if they want their instances to be Serialized or Deserialized.
String does not implement scala. Serializable. There are no dependencies from Java to Scala.
or do I miss something important?
Yes, you do.
readObject/writeObject methods have to be private and NOT overridden for the mechanism to work properly.
They are called in reverse way (i.e. superclass->class) too. Morealso you do wish that the method remains private to prevent misuse (and explicit calls)
Serializable mechanism offers other methods as well: like writeReplace+readResolve, that are usually not used in the same class + readObjectNoData (that's not so useful).
Now if you need specific method take a look at java.io.Externalizable. It does publish its methods and implementing it overrides the default serialization mechanism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With