• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Overriding the write/readObject methods when doing serialization.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 6 of K&B talks about overriding the write/readObject methods for serialization with private methods of the same name. I have two questions about that.
1) Why are the overridding methods private? I didn't think you could override a method with a private one.
2) When calling the original methods, why do you have to use defaultWrite/ReadObject? What's wrong with using super?
I'm sure I'm missing something; I just don't know what.

Thank you for your time.
John Daniel,
Carrollton, Texas
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
I think your query is related to custom serialization.
Suppose you want some state values of that object at the time of deserialization but the object itself is not serializable then how you save those states in serialization process if that vlaues are needed when object is deserialized so you can use those private method signatures in the class of that object so that you can save state values of that object manually. This is the place where custom serialization happens. In such a way that you can obtain the state value of that object manually in the custom serialization even if the object itself is not serializable. You just skip serialization of that object using transient keyword.if you don't use custome serialization your object's state values are lost and you get default state values in deserialization process.

If you can't understand this explaination refer kathy sierra's SCJP guide and Java API documentation.

I hope this clears
 
Master Rancher
Posts: 5005
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Daniel wrote:1) Why are the overridding methods private? I didn't think you could override a method with a private one.


You can't. This is not overriding. If K&B says it's overriding, that's incorrect. But perhaps they said it's like overriding, which is true enough. You're required to match a given set of method signatures exactly, which is quite similar to overriding. But it's not the same thing.

Basically, Java's creators bent the rules here, because they didn't want to force users to make non-private methods exposing private data, just to support serialization. And moreover, no one should ever really use these methods, except the JVM itself. So they chose to make the methods private.

The rules for serialization are special, in that they don't really follow other rules for the Java language (like how overriding works). The serialization rules only work because the people who made the rules also had control of the implementation of the whole JDK including the JVM - so they were able to make special code inside the JVM just to handle this. Don't try this at home.

John Daniel wrote:2) When calling the original methods, why do you have to use defaultWrite/ReadObject? What's wrong with using super?


Since you aren't actually overriding anything, super would not work.
 
John Daniel
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your prompt and informational reply. John
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic