This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 1950
    
  28
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/
 
 
subject: Deserialization of child object
 
Similar Threads
Method invocation without Object
Rigidity of type casting
ClassCastException on downcasting
Overriding/overloading
Why cant a abstract class be instantiated?