Abhijeet Kale
Abhijeet Kale

Reputation: 1716

trying to Serialize parents member variables from child where parent does not implement Serializable

I am aware that you cannot serialize parent's member variables if parent does not implement Serializable interface.

I have created a sample program and trying to find a way (no matter how hacky it is) to serialize parent classe's member variables without changing it to implement serializable.

below is the code :

class s
{ 
    int i;
    public int getI() {
        return i;
    }
    public void setI(int i) {
        this.i = i;
    }
    public int getJ() {
        return j;
    }
    public void setJ(int j) {
        this.j = j;
    }
    int j;
    private int fun(){
         return 1;
     }
}

class m extends s implements Serializable
{
    int k;
    int l;
    public int getK() {
        return k;
    }
    public void setK(int k) {
        this.k = k;
    }
    public int getL() {
        return l;
    }
    public void setL(int l) {
        this.l = l;
    }

    public int fun(){
        return 2;
    }
}

class n1{
    public static void main(String...s)
    {
        m m1  = new m();
        m1.setI(100);
        m1.setJ(101);
        m1.setK(401);
        m1.setL(701);

        try {
            OutputStream os = new FileOutputStream(new File("d:/aaa.data"));
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(m1);
            InputStream is = new FileInputStream(new File("d:/aaa.data"));
            ObjectInputStream ois = new ObjectInputStream(is);
            m m2 = (m)ois.readObject();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
}

I would appreciate your thoughts on this matter with upvote for sure :) thanks

Upvotes: 1

Views: 143

Answers (2)

Abhijeet Kale
Abhijeet Kale

Reputation: 1716

Answering my own question.

Thanks to @Ramanlfc came to know Serialization Proxy Pattern.

Serialization Proxy Pattern comes handy for conditions exactly like this;

here is the code that implements Serialization Proxy pattern in above code.

class m extends s implements Serializable {
    int k;
    int l;

    public m() {
    }

    public m(int i, int j, int k, int l) {
        this.i = i;
        this.j = j;
        this.k = k;
        this.l = l;
    }

    private static class MProxy implements Serializable {

        int i;
        int j;
        int k;
        int l;

        public MProxy(m point) {
            this.i = point.getI();
            this.j = point.getJ();
            this.k = point.getK();
            this.l = point.getL();
        }

        private Object readResolve() {
            return new m(i, j, k, l);
        }

    }

    private Object writeReplace() {
        return new MProxy(this);
    }

    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
    }

    public int getL() {
        return l;
    }

    public void setL(int l) {
        this.l = l;
    }

    public int fun() {
        return 2;
    }
}

to implement this pattern I added a new class MProxy. which is a kind of container that contains copy of state of child class.

and wrote a method writeReplace() to override default serialization writeobject behaviour. It now return instance of MProxy instead of this which is then written to the file.

here no changes were made to parent class.

Upvotes: 0

user207421
user207421

Reputation: 310883

You can solve this by adding the following methods to the child class:

  1. A writeObject() method that also serializes the members of the non-serializable class.
  2. A readObject() method that deserializes them.

The required signatures of these methods and what they can/must do are defined in the Object Serialization Specification.

Upvotes: 1

Related Questions