• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization - overriding readObject()

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic