• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object serialization

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Dave. Your answer is very helpful.
 
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic