java fans: If I extend a class which cann't be modified and didn't implements seriable. When I serialize my object, it didn't serialize the member variable inherited from parent class. Are there methods to serialize the object including the parent class without modifying the parent class? Any help will be appreciated. Please see the foll. snippet. =============================================== import java.io.*; class AA{ int x=11; public void setx(int a){x=a;} }
class BB extends AA implements Serializable{ int y=22; public void sety(int a){y=a;} }
It may not be very OO, but you could always add the same variable to your subclass and then do something like
in your constructor. That would place the x variable and its value in the Serializable class.
Dave Turner
Ranch Hand
Joined: Mar 13, 2001
Posts: 60
posted
0
from the Java 1.3 API, java.io.serialization Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:
So you can write your own object serialization methods for your class BB to control the serialization of the variable x in your non-serializable class AA. You will need to use writeObject to output the values of x and y in Integer objects, and readObject to read them in again. the following changes to your class BB should make the serialization work how you want it to.