Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Serialization with respect to Inheritance

Case-1:-

Eventhough child class doesn't implement serializable we can serialize child class object is parent class implements serializable interface that is serializable nature is inheriting from parent to child hence if parent is serializable then by default every child is serializable.

Example:
import java.io.*;
class Animal implements Serializable
{
	int i=10;
}
class Dog implements Animal
{
	int j=20;
}
class SerializeDemo5
{
	public static void main(String[] args)throws Exception
	{
		Dog d1=new Dog();
		System.out.println(d1.i+"'"+d1.j);
		FileOutputStream fos=new FileOutputStream("abc.ser");
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		oos.WriteObject(d1);
		FileInputStream fis=new FileInputStream("abc.ser");
		ObjectInputStream ois=new ObjectOutputStream(fis);
		Dog d2=(Dog)ois.readObject();
		system.out.println(d2.i+"'"+d2.j);
	}
}

In the above example eventhrough dog class doesn't implement serializable we can serialize Dog object because its parent Animal class implements serializable.

Notes:

Object class doesn't implement serializable interface.

Case-2:

  • • Eventhough parentclass doesn't implement serializable we can serialize child class object if child class implements serializable interface that is to serialize child class object parent class need not be serializable.
  • • At the time of serialization JVM will check if any variable inheriting from non serializable parent or not.If any variable inheriting from non serializable parent then JVM ignores original value and save default value to the file.
  • • At the time of deserialization JVM will check is any parent class nonserializable or not.If any class is non serializable than JVM will execute instance control flow in every non srializable parent and share its instance variable to the current object.
  • • While executing instance control flow of non serializable parent JVM will always call no-argument constructor.Hence every nonserializable class should compulsorily contain no argument constructor,It may be default constructor generated by compiler(or)customized constructor explicitly provided by programmer. Otherwise we will get RuntimeException saying InvalidClassException.
Example:
import java.io.*;
class Animal
{
	int i=10;
	Animal()
	{
		System.out.println('Animal constructor called');
	}
	class Dog extends Animal implements Serilaizable
	{
		int j=20;
		Dog()
		{
			System.out.println('Dog constructor called');
		}
		class SerializableDemo6
		{
			public static void main(String[] args)throws Exception
			{
				Dog d1=new Dog();
				d2.i=888;
				d2.j=999;
			}
		}
		FileOutputStream fos=new FileOutputStream('abc.ser');
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		oos.writeObject(d1);
		System.out.println('Deserialization statred');
		FileInputStream fis= new FileInputStream('abc.ser');
		ObjectInputStream ois=new ObjectInputStream(fis);
		Dog d2=(Dog)ois.readObject();
		System.out.println(d2.i+''.'+d2.j);
	}
}

Output:

Animal constructor called
Dog constructor called
Deserialization started
Animal constructor called
0'..999

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext