Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How it works when we implement writeObject and readObject

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer the below program:



Output:


Please help me to understand the lines 53 and 61

I have implemented the writeObject() and readObject() methods for the Dog class. The
line 53: I am passing the Dog object to writeObject() method. So it could somehow use the Dog reference and get access to the writeObject() that is overridden.
line 61: I am using the ObjectInputStream reference variable to call the readObject() method. I am casting it to Dog after the object is returned by readObject method.
How could the ObjectInputStream reference variable get access to a method that is available in Dog class. I am not passing the dog object as an argument to readObject.
Could someone help to understand the ObjectInputStream gets access to Dog's readObject() method.

Thanks
Loganathan


 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is said here http://java.sun.com/developer/technicalArticles/Programming/serialization, that:

1. No new object is created during de-serialization process, just already persisted object is read;
2. The virtual machine will automatically check to see if either method (i.e. readObject and writeObject) is declared during the corresponding method call. The virtual machine can call private methods of your class whenever it wants but no other objects can.
 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic