• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

De-Serializing Objects

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have basically two classes: Animal
|
Monkey (extends Animal)

Basically �Monkey� class implements Serializable interface, but Animal does not. Animal Has �name� attribute, and Monkey inherits this instance variable from Animal. �name� attribute has a Default value of �animal�

I created a Monkey Object { Animal a_obj = new Monkey(�Monkey�) } ,so now we have an Animal Reference Variable referring to a Monkey Object, where the name attribute is set to �Monkey�. I used the ObjectOutputStream to Serialize it, which was fine. Now the strange thing occurs when ever I De-Serialize the same object. The De Serialized Object of Monkey (which was named �Monkey�), Referenced by the Animal Reference Variable, the value of the Monkey name attribute is changed to its Default value i.e �animal�. It seems that the Constructor of the Parent class (Animal) is invoked when ever we desecrialize the Monkey Object, which is why the name is is assigned its Default value.

Thanks
Obaid Salikeen
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you read the API for java.io.Serializable you will see that it is perfectly normal expected behaviour for that to happen.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would need a private void writeObject(ObjectOutputStream os) method. Default serialize the object and then serialize the name attribute. Reading a bit about the serialization mechanism from API would help a lot. Start from here.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:
Monkey
animal

Uncomment Line #1
Output:
Monkey
Monkey

"Member variables are not called polymorphically."


Thanks,
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> "Member variables are not called polymorphically."

True, but I don't see any connection to situation Obaid described.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Stone:
>> "Member variables are not called polymorphically."

True, but I don't see any connection to situation Obaid described.



I said that in connection to the comment/uncomment Line #1 (marked bold).
I thought If Obaid would be under impression of such an case I described
above. His case is genuine, what the rule of Serialization says, if the
parent class is not Serializable, while deserializing child class object,
the constructor of the Parent class is called.

If Monkey class had its own name variable, as well as overridden toString()
method, the "Monkey" would be printed; otherwise, while deserailizing
Monkey, constructor of the Animal is called and we get name ="animal",
irrespective even if we have name in the Monkey class as well.


Thanks,
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, I see the point now. Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic