soc
soc

Reputation: 28433

Why does scala.Serializable not specify any methods?

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?

Upvotes: 12

Views: 712

Answers (3)

user207421
user207421

Reputation: 311008

There are no methods to make public in serialization. There are four methods you may implement but they are all optional. Your question doesn't make sense.

Upvotes: -1

bestsss
bestsss

Reputation: 12056

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.

Upvotes: 5

antlersoft
antlersoft

Reputation: 14786

Answering from a Java background but I would guess the same reasoning applies to Scala: Java does not require methods marked Serializable to implement any methods; the runtime provides the serialization mechanism itself. That's why the interface is empty. readObject and writeObject are not part of Serializable not because they aren't public but because objects derived from Serializable don't need to implement them.

Serializable really shouldn't be an interface but an annotation (especially because a class derived from a Serializable class may very well not be Serializable itself), but it was part of the language before the language had annotations.

Upvotes: 5

Related Questions