• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with Object Desirialization

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i was reading the serialization chapter of Head First Java where I read

If the object has a non-serializable class
somewhere up its inheritance tree, the
constructor for that non-serializable class
will run along with any constructors above
that (even if they're serializable).




To try this out, i wrote the following sample code.





This code worked as expected. the output was

A Constructed
B Constructed
C Constructed
c=34




But the problem appeared in deserialization. I just replaced the main() method by


now the output is
c=34

while constructors of B & C were supposed to be called.
Can anyone please explain what's wrong?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During the deserialization a constructor is not called. Everything is ok with the example and your code as well.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class A is serializable, and therefore through inheritance so is B. Remove that interface from class A and you'll see what you expect.
 
Sudhanshu Gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Rob & Kate.
I understood it. But one thing that still bothers me is
if through inheritence, B also becomes serializable as A is serializable,
how is it possible to have an inheritence tree where a class is not serializable
even if there is a serializable class up in the heirarchy as quoted

If the object has a non-serializable class
somewhere up its inheritance tree, the
constructor for that non-serializable class
will run along with any constructors above
that (even if they're serializable).



I hope i m not being stupid. Just a bit confused.
Thanks again for your help.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If class A is not serializable, then class C has three classes up in the hierarchy that are not serializable - B, A and Object.

At the moment there is only Object, and that has a parameter-less constructor, so no problem there.
 
Sudhanshu Gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Rob.
I got the point.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the object has a non-serializable class
somewhere up its inheritance tree, the
constructor for that non-serializable class
will run along with any constructors above
that (even if they're serializable).




This means that if you serialize any object whose any of the non serializable parent (who do not implement the Serializable interface) then at time of deserializtion the constructors of non serializable parent will only run......
example

This Code will gonna produce following output:

 
Scott Bravo
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the object has a non-serializable class
somewhere up its inheritance tree, the
constructor for that non-serializable class
will run along with any constructors above
that (even if they're serializable).




This means that if you serialize any object whose any of the non serializable parent (who do not implement the Serializable interface) then at time of deserializtion the constructors of non serializable parent will only run......
example

This Code will gonna produce following output:

Note in this example if I also serialize "the class A and class B" or I ll serialize "only class A"..then the output would be


so if you want to run the parent class constructors at the time of "deserialization" ...then do not implement "serializable" interface in parent classes
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic