Learnitweb

Serialization with respect to inheritance

Case 1: Parent class implements Serializable

When parent class is serializable then child class is also serializable. That is if parent class implements Serializable interface then this behavior is inherited to child. Hence, it is not required by child class to explicitly implement Serializable interface.

Case 2: Parent class does not implement Serializable but Child class implements Serializable

  • If parent class does not implement Serializable but we can serialize child object if child class implements Serializable interface. An example of this is Object class. Object class does not implement Serializable but any class which is child of Object class can implement Serializable.
  • While serializing sub-class, JVM will check if there is any super class which isn’t implementing java.io.Serializable interface. Then, inheriting instance variables of non-serializable super class will be stored to default value ignoring their original values like 0 for Integer, null for String etc.
  • At the time of deserialization JVM checks whether any parent class is non Serializable or not. If any parent class is non Serializable, JVM creates a separate object for every non Serializable parent and shares its instance variables to the current object. During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. Every non Serializable parent should compulsory contain no arg constructor otherwise we will get runtime exception “InvalidClassException”. Constructor can be user provided or jvm created. We don’t need a no-arg constructor while parent is serializable.
  • If non-serializable parent is abstract class then instance control flow will be performed and share it’s instance variable to the current object.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Parent {
	int parentVariable=100;
	Parent(){
		System.out.println("Parent constructor called");
	}
}

class Child extends Parent implements Serializable{
	int childVariable=120;
	Child(){
		System.out.println("Child constructor called");
	}
}


public class CustomSerializationExample {
	public static void main(String args[]) throws Exception {
		FileOutputStream fos=new FileOutputStream("test.ser"); 
		ObjectOutputStream oos=new ObjectOutputStream(fos); 
		
		Child child1 = new Child();
		
		oos.writeObject(child1); 
		System.out.println("Deserialization started"); 
		FileInputStream fis=new FileInputStream("test.ser"); 
		ObjectInputStream ois=new ObjectInputStream(fis); 
		Child child2=(Child)ois.readObject(); 
		System.out.println("Parent variable: " + child2.parentVariable);
		System.out.println("Child variable: " + child2.childVariable);
	}
}

Output

Parent constructor called
Child constructor called
Deserialization started
Parent constructor called
Parent variable: 100
Child variable: 120