wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Problem with Object Desirialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem with Object Desirialization " Watch "Problem with Object Desirialization " New topic
Author

Problem with Object Desirialization

Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
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?

Thanks & Regards

Sudhanshu Gupta

Kate Terlecka
Greenhorn

Joined: May 13, 2009
Posts: 12
During the deserialization a constructor is not called. Everything is ok with the example and your code as well.


--
Never argue with an idiot.
He will bring you down to his level
and beat you with experience.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Class A is serializable, and therefore through inheritance so is B. Remove that interface from class A and you'll see what you expect.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
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

Joined: Oct 27, 2005
Posts: 19759
    
  20

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

Joined: May 02, 2009
Posts: 23
Thank you very much Rob.
I got the point.
Scott Bravo
Greenhorn

Joined: Jun 15, 2009
Posts: 18
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

Joined: Jun 15, 2009
Posts: 18
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Object Desirialization