Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization - overriding readObject()

 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

I am trying to understand the overriding of writeObject() and readObject() to manually serialize non-serializable members of a class.

I am writing the class instance into a file in one java program and then reading it using a different java program. I have overridden the writeObject() and readObject(). Overridden writeObject() works fine, but when I try to read the saved object, the overridden readObject() is not called at all. I am unable to see why it is not working. Could you please identify where is the problem in the following code?

1. SerTest.java - serializes the object. I am overriding wireObject() to manually serialize a static variable in the class.



2. ReadSerTest.java - de-serialize the object. The problem is here - overridden readObject() is not being called at all.



I can not see the output "Reading static variable manually...", it means it is not going to the overridden readobject() method.
 
Henry Wong
author
Marshal
Pie
Posts: 20897
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not see the output "Reading static variable manually...", it means it is not going to the overridden readobject() method.


To deserialize a SerTest object, it will use the readObject() method, if available, of the SerTest class. It will use the readObject() method, of the ReadSerTest class, when deserializing a ReadSerTest object.

Henry
 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved the readObject() method to SerTest.java. Worked like a charm!

Thank you so much.

From the example in the k&b book it was not clear to me that both the readObject() and writeObject() overrides should be in the same class (it looks so obvious now as I am writing it!)

Here is the complete listing if somebody wants to try it out.

1. SerTest.java



2. ReadSerTest.java

 
harsha balluru
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I correct something here:
Kiran,
By defining the methods readObject() and writeObject(), you actually "implement" the methods; You can NOT "override" them. These methods you are implementing here come from the "Serializable" interface implemented by the class whose object's state you are trying to save.
In fact the classes java.io.ObjectOutputStream and java.io.ObjectInputStream have declared the methods writeObject() and readObject() as final.
 
Kushan Athukorala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kiran,

I am adding following to harsha's comments.

Since no methods are defined in Serializable interface, such interfaces are called Marker Interfaces.

Remember this in the exam. Serializable and RandomAccess are marker interfaces

Thanks,
Kushan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic