Inheritance in Serialization
In this tutorial, we are going to discuss about Inheritance in Serialization. If the parent class is Serializable by default all the child classes also Serializable. i.e Serializable nature is inherited from parent to child.
Hence even though child class doesn’t implements Serializable, we can serialize child class object if parent class implements serializable interface.
Inheritance in serialization refers to the process of serializing and deserializing classes that are part of an inheritance hierarchy. When serializing a class hierarchy, including subclasses, there are several considerations to ensure correct serialization and deserialization behavior.
package com.ashok.files;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author ashok.mariyala
*
*/
class Animal implements Serializable {
int a = 10;
}
class Dog extends Animal {
int b = 20;
}
public class MySerialization {
public static void main(String arg[]) throws Exception {
Dog d = new Dog();
FileOutputStream fos = new FileOutputStream("ashok.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("ashok.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog) ois.readObject();
System.out.println(d1.a + " ----- " + d1.b);
}
}
Output
10 ----- 20
If the child class is Serializable and some of the parent classes are not Serializable, Still we are allowed to serialize child class Objects. While performing serialization JVM ignores the inherited variables which are coming from non-serializable parents. While performing de-serialization JVM will check is there any parent class is non-serializable or not.
If any parent is non-serializable JVM will create an object for every non-serializable parent and share it’s instance variables for the current child object. The non-serializable parent class should compulsory contain no-argument constructor otherwise we will get runtime error.
package com.ashok.files;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author ashok.mariyala
*
*/
class Animal {
int i = 10;
Animal() {
System.out.println("Animal Constructor");
}
}
class Dog extends Animal implements Serializable {
int j = 20;
Dog() {
System.out.println("Dog Constructor");
}
}
public class MySerialization {
public static void main(String arg[]) throws Exception {
Dog d = new Dog();
d.i = 888;
d.j = 999;
System.out.println(d.i + "-----" + d.j);
System.out.println("Serilization Started");
FileOutputStream fos = new FileOutputStream("ashok.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
System.out.println("Deserialization Started");
FileInputStream fis = new FileInputStream("ashok.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog) ois.readObject();
System.out.println(d1.i + "-----" + d1.j);
}
}
Output
Animal Constructor
Dog Constructor
888-----999
Serilization Started
Deserialization Started
Animal Constructor
10-----999
That’s all about the Inheritance in Serialization. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!