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.