• 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

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: 22783
131
Eclipse IDE Spring VI Editor 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: 22783
131
Eclipse IDE Spring VI Editor 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 was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic