This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.