Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Object Desirialization

 
Sudhanshu Gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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?
 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During the deserialization a constructor is not called. Everything is ok with the example and your code as well.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much Rob.
I got the point.
 
Scott Bravo
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic