• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deserialization of child object

 
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see this thread. Might be helpful.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic