Object Serialization with inheritance in Java

Java 8Object Oriented ProgrammingProgramming

In Serialization when inheritance is introduced then on the basis of superclass and subclass certain cases have been defined which make the understanding of Serialization in each case much simpler. The fundamental rules which should be followed are as below.

1. When super class is implements Serializable interface and subclass is not.

In this case the object of subclass get serialized by default when superclass get serialize, even if subclass doesn't implements Serializable interface.

Example

public class TestSerialization {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      B obj = new B();
      FileOutputStream fos = new FileOutputStream("abc.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(obj);
      FileInputStream fis = new FileInputStream("abc.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      B obj1 = (B)ois.readObject();
      System.out.println(obj1.i + " " + obj1.j);
   }
}
class A implements Serializable {
   int i = 10;
}
class B extends A {
   int j =20;
}

Output

value of i is : 10 & value of j is : 20

2. When super class is not implementing serializable interface and subclass implements.

In this case super class instances variables which are inherited in subclass doesn't get serialize and also looses their assigned values during serialization of sub class.Also JVM during serialization of subclass re-assign the default initialized values to these instance variables of superclass.One more point which is to be taken care of it in this scenerio is super class must have default no argument constructor as JVM access super class during deserialization.In case this constructor is not present a compile time exception will be encountered.

Example

public class TestSerialization {
   public static void main(String[] args) throws IOException,ClassNotFoundException {
      B obj = new B(10,20);
      FileOutputStream fos = new FileOutputStream("abcd.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(obj);
      FileInputStream fis = new FileInputStream("abcd.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      B obj1 = (B) ois.readObject();
      System.out.println("value of i is : " +obj1.i + " & value of j is : " + obj1.j);
   }
}
class A {
   int i;
   A() {
      System.out.println("default constructor called of parent class.");
   }
   A(int i) {
      this.i=i;
   }
}
class B extends A implements Serializable {
   int j;
   B(int i , int j) {
      super(i);
      this.j=j;
   }
}

Output 1

When default constructor is present.

default constructor called of parent class.
value of i is : 0 & value of j is : 20

Output 2

When default constructor is not present.

Exception in thread "main" java.io.InvalidClassException: B; B; no valid constructor

3. When requirement is to serialize superclass but not to subclass (Custom Serialization).

In order to prevent subclass from serialization we need to implement writeObject() and readObject() methods which are executed by JVM during serialization and deserialization also NotSerializableException is made to be thrown from these methods.We can also provide our custom logic in these methods which would be executed during serialization/deserializatin.

Example

public class TestSerialization {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      B obj = new B();
      FileOutputStream fos = new FileOutputStream("abc.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(obj);
      FileInputStream fis = new FileInputStream("abc.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      B obj1 = (B)ois.readObject();
      System.out.println("value of i is : " + obj1.i + " & value of j is : " + obj1.j);
   }
}
class A implements Serializable {
   int i = 10;
}
class B extends A {
   int j =20;
}
// implementing writeObject method,
private void writeObject(ObjectOutputStream out) throws IOException {
   throw new NotSerializableException();
}
// implementing readObject method,
private void readObject(ObjectInputStream in) throws IOException {
   throw new NotSerializableException();
}

Output

Exception in thread "main" java.io.NotSerializableException
at B.writeObject(A.java:20)
raja
Published on 01-Nov-2018 15:34:01
Advertisements