1 2 Previous Next 18 Replies Latest reply: Aug 16, 2010 10:28 AM by 843790 RSS

    Understand the overriding of writeObject() and readObject() to manually

    843790
      Hi,

      I am trying to understand when to use overriding of writeObject() and readObject() methds and when ObjectStreamField.

      It will be good if any one can explain it me with example or programming scenario :)

      Below I have two classes:

      MathOperation: non-serialisable
      SerializeMathOperationClass: non-serialisable


      MathOperation Class
      package myClassSerialization;
      
      
      public class MathOperation {
           private int x;
           private int y;
           
           public MathOperation() {
                
           }
           
           public void setX(int x) {
                this.x = x;
           }
           public int getX() {
                return x;
           }
           public void setY(int y) {
                this.y = y;
           }
           public int getY() {
                return y;
           }
      }
      SerializeMathOperationClass Class
      package myClassSerialization;
      
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      
      
      public class SerializeMathOperationClass {
      
           private MathOperation mathOperation = null;
           
           public void initializeData(int x, int y) {
                mathOperation = new MathOperation();
                mathOperation.setX(x);
                mathOperation.setY(y);
           }
           
           public void serializeTheMathOperation() throws IOException {
                System.out.println("Creating File/Object output stream...");
              
              FileOutputStream fileOut = new FileOutputStream("MathOperation.ser");
              ObjectOutputStream out = new ObjectOutputStream(fileOut);
              
              out.writeObject(mathOperation);
              
              out.flush();
              fileOut.flush();
              out.close();
              fileOut.close();
              
           }
           
           public void deserializeTheMathOperation() throws IOException, ClassNotFoundException {
                MathOperation localMath = null;
                
                System.out.println("Creating File/Object input stream...");
              
              FileInputStream fileIn = new FileInputStream("MathOperation.ser");
              ObjectInputStream in = new ObjectInputStream(fileIn);
              
              localMath = (MathOperation) in.readObject();
              
              System.out.println("X = " + localMath.getX() + "\tY = " + localMath.getY());
              
              in.close();
              fileIn.close();
           }
           
           private void writeObject(ObjectOutputStream oOutS) throws IOException {
                oOutS.defaultWriteObject();
                oOutS.writeObject(mathOperation);
                System.out.println("Inside overriding writeObject");
           }
           
           private void readObject(ObjectInputStream oInS) throws IOException, ClassNotFoundException {
                oInS.defaultReadObject();
                System.out.println("Inside overriding readObject");
           }
           
           public static void main(String[] args) throws IOException, ClassNotFoundException {
                SerializeMathOperationClass serializeDemo = new SerializeMathOperationClass();
                serializeDemo.initializeData(10,15);
                serializeDemo.serializeTheMathOperation();
                serializeDemo.deserializeTheMathOperation();
           }
      }
      Here I m trying to serialize the mathOperation field of SerializeMathOperationClass but I am getting the below error.
      Creating File/Object output stream...
      Exception in thread "main" java.io.NotSerializableException: myClassSerialization.MathOperation
           at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
           at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
           at myClassSerialization.SerializeMathOperationClass.serializeTheMathOperation(SerializeMathOperationClass.java:27)
           at myClassSerialization.SerializeMathOperationClass.main(SerializeMathOperationClass.java:66)
        1 2 Previous Next