ps-aux
ps-aux

Reputation: 12174

Why does not Serializable interface require to implement readObject() and writeObject()

Why is Serializable only marker interface and does not require by contract implementing readObject() and writeObject(). Wouldn't it make more sense?

Upvotes: 0

Views: 451

Answers (1)

JB Nizet
JB Nizet

Reputation: 692121

The serialization mechanism is able to serialize an object without any readObject() and writeObject() methods. But it requires you to specify it objects can or can't be serialized. Some objects hold data, that it makes sense to serialize. Some others don't. For example, serializing a String makes sense, but serializing a Socket or a database Connection doesn't. So you simply mark which objects may be serialized by making their class implement the Serializable interface.

If you want more control over the way the state of the object is serialized and deserialized, then you can use these two methods.

Upvotes: 2

Related Questions