Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question related to serialization.

 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While reading a document on serialization i found that there are two call back methods readObject(ObjectInputStream) and writeObject(ObjectOutputStream) are present which are called by the JVM just before the object is serialized and deserialized.

I just wanted to know that when these call back methods are already present then why do we have need for Externalizable interface when we can do the same from these callback methods.
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of implementing Serializable interface,JVM does the serialization for us using readObject(ObjectInputStream) and writeObject(ObjectOutputStream) methods.

We may override these methods in the class being serialized to handle extra functionalities (like handling transient variables).We still have to call to call defaultReadObject () and defaulWriteObject () methods to get the actual object serialized/deserialized.

In the case of implementing Externalizable interface, we need to write code to save the contents... JVM won't do it for us.





 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john sal wrote:
I just wanted to know that when these call back methods are already present then why do we have need for Externalizable interface when we can do the same from these callback methods.

Externalization allows you to control what need to serialized. It’s the programmer responsibility to write the class data to the stream (as well the super class, if any).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic