aspose file tools*
The moose likes I/O and Streams and the fly likes Deserialization of child object 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 "Deserialization of child object" Watch "Deserialization of child object" New topic
Author

Deserialization of child object

Ramin Esfandiari
Greenhorn

Joined: Mar 15, 2013
Posts: 16

I have a class (B) which extends parent (A) which is serializable. Object A has a generated SerialVersionUID but B does not.

When reading an ObjectInputStream that should contain objects of type B, I sometimes get objects of type A.
Why is that?
I have a theory that it is because class B is missing a SeriealVersionUID so when deserializing the parent type (A) is read instead of the child type (B) and if this is the case why does this only happen occasionally?



Thanks
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
I have a theory that it is because class B is missing a SeriealVersionUID so when deserializing the parent type (A) is read instead of the child type (B)

If you don't specify a SerialVersionUID the the compiler calculates one for you so class B isn't missing a SerialVersionUID it just doesn't have one that you have specified.
There are potential problems with not providing your own SerialVersionUID but these generally result in an InvalidClassException being thrown.

Without seeing an SSCCE it's hard to say exactly what is happening. Can you supply one or more information?
Can you show a small serialized file that is exhibiting this behaviour.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Also see this thread. Might be helpful.


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deserialization of child object