• 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

Where should we implement readObject() and writeObject()

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 15 of chapter 6 in K&B reads that "you would implement the readObject() method in SpecialSerial".
I am trying to understand why implement it in SpecialSerial as opposed to TestSer, where the serialization/deserialization is done.

I didn't find any explanation in the book.
Thanks for your help.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

This is a good question. See pages 449-452 for an explanation.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most likely, you're confused because there are actually two different "readObject()" methods being used here. One is the "Object readObject()" method of ObjectInputStream, which is what TestSer is invoking. However, you can also implement a different readObject() method in the Serializable class itself to customize the serialization process. This other readObject() method has a different signature from the one invoked by TestSer: "void readObject(ObjectInputStream in)". Same thing with writeObject().

I always thought it was a little unfortunate that Java's designers decided to use the same name for those two methods. Something like "readObject()" and "readThisObject()" would have been a bit better, in my opinion.
 
reply
    Bookmark Topic Watch Topic
  • New Topic