• 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

Using inheritance to serialize an object containing a non-serializable reference

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a Dog class which is serializable. It contains the reference of Collar class which is not serializable. I need to serialize a dog object ( I'm not making collar reference a transient variable and I am not using call back methods ) and reconstruct the dog object after deserializing it. The text I have been referring to suggests I can use inheritance for this. So I have coded my class as follows. I guess the serialization part is working fine.



I think there is something wrong with the deserialization part. I'm not sure if I have coded the deserialization block correctly. I think I need to construct a new Collar2 object and a dog object and I'm not sure how this can be done. When I executed the above code, I got the following output.


C:\SCJP Practice Demos>java SerializeDog
Dog@6e34b350
50
5
java.io.InvalidClassException: Collar2; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unkn
own Source)
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at SerializeDog.main(SerializeDog.java:24)
Dog@6e34b350
50
5

Could somebody please advice?

Thanks.

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just read that deserialization will not restore collar variable to its state at the time of serialization as deserialization would always invoke the default constructor in this case.

And I was getting an exception cause Collar class does not have a default constructor as I have coded a constructor with parameters. I changed the collar class as follows.



and got the following output.


C:\SCJP Practice Demos>javac SerializeDog.java

C:\SCJP Practice Demos>java SerializeDog
Dog@45e881b6
50
5
Dog@2bc68b6f
50
0

I guess we can't use inheritance for the above case. I'm not sure though. If somebody could confirm, this topic is RESOLVED. Thanks.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Is there more I should read on the subject? Is my understanding incorrect/incomplete? Or is my question really stupid? Or will the answer contain too many things to be answered in a forum post?

Let me put it this way- If I do not change the way I have serialized the dog object, can I tweak the deserialization logic in any way to get the correct value for the collar reference( I mean can I override the API behaviour specific to it invoking the default constructor for the collar reference).

Or should I conclude that inheritance alone cannot help in deserializing an object that contains the reference of a non serializable object.

Please advice.

Thanks very much.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic