• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Object serialization  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;}
}

public class test{
public static void main(String args[]) {
BB b=new BB();
b.setx(33);
b.sety(44);
try{
FileOutputStream fos=new FileOutputStream("xxx.yyy");
ObjectOutputStream oos=new ObjectOutputStream(fos);
System.out.println("befor: "+b.x+" and "+b.y);//33,44
oos.writeObject(b);
oos.close();
fos.close();
}catch (IOException e){
System.out.println("error write obj");
}
try{
FileInputStream fis=new FileInputStream("xxx.yyy");
ObjectInputStream ois=new ObjectInputStream(fis);
BB sb=(BB)(ois.readObject());
System.out.println("after: "+sb.x+" and "+sb.y);//11,44
ois.close();
fis.close();
}catch (IOException e){
System.out.println("error read obj");
}catch(ClassNotFoundException e){
}
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

HTH
Dave
 
Allan Wang
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Dave. Your answer is very helpful.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!