aspose file tools*
The moose likes Beginning Java and the fly likes Using inheritance to serialize an object containing a non-serializable reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using inheritance to serialize an object containing a non-serializable reference" Watch "Using inheritance to serialize an object containing a non-serializable reference" New topic
Author

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

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
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
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
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
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using inheritance to serialize an object containing a non-serializable reference