Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deserialization of child object

 
Ramin Esfandiari
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see this thread. Might be helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic