I have the following class hierarchy
ParentInterface
|
v
ParentClass
/ \
v v
SubClass1 SubClass2
The interface does not extend Serializable
interface and none of the classes implement Serializable
interface as well. All the classes were compiled and packaged into a jar file.
Now, i change the ParentInterface
so that it extends Serializable
interface. After this, i only replace the ParentInterface.class
file in the jar.
I have some code where I am trying to Serialize an instance of SubClass1
and SubClass2
. When trying to serialize, i get NotSerializableException
.
I assume, if i had compiled all the classes again and replaced them in my jar, it would have worked. Whats wrong if i replace only the 开发者_如何学运维ParentInterface
?
All subtypes of a serializable class are themselves serializable.
According to JavaDoc, the SubClass1 and SubClass2 should be serializable.. unless they have some fields, that are not serializable.
Double check the serializablity of the fields inside SubClass1
and SubClass2
.
This may help..
SubClass1 and SubClass2 will by definition be serializable
However as advised pay particular attention to fields contained in both SubClass1 and SubClass2. Paying attention to the below restriction described in the JDK.
During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.
Be sure to make "un-serialisable" fields as transient or act as appropriate.
精彩评论