File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization - overriding readObject() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization - overriding readObject()" Watch "Serialization - overriding readObject()" New topic
Author

Serialization - overriding readObject()

Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
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.


Kiran
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
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

Joined: Feb 06, 2009
Posts: 14
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

Joined: Aug 09, 2010
Posts: 33
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


Kushan Athukorala
OCPJP 6.0 [86%]
 
 
subject: Serialization - overriding readObject()