Object Serialization with Inheritance in Java
Last Updated :
09 Dec, 2021
Prerequisite: Serialization, Inheritance
Serialization is a mechanism of converting the state of an object into a byte stream. The byte array can be the class, version, and internal state of the object.
Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
There are some cases of Serialization with respect to inheritance:
Case 1: If the superclass is serializable, then subclass is automatically serializable
If the superclass is Serializable, then by default, every subclass is serializable. Hence, even though subclass doesn't implement Serializable interface( and if its superclass implements Serializable), then we can serialize subclass object.
Java
// Java program to demonstrate
// that if superclass is
// serializable then subclass
// is automatically serializable
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
// superclass A
// implementing Serializable interface
class A implements Serializable
{
int i;
// parameterized constructor
public A(int i)
{
this.i = i;
}
}
// subclass B
// B class doesn't implement Serializable
// interface.
class B extends A
{
int j;
// parameterized constructor
public B(int i, int j)
{
super(i);
this.j = j;
}
}
// Driver class
public class Test
{
public static void main(String[] args)
throws Exception
{
B b1 = new B(10,20);
System.out.println("i = " + b1.i);
System.out.println("j = " + b1.j);
/* Serializing B's(subclass) object */
//Saving of object in a file
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// Method for serialization of B's class object
oos.writeObject(b1);
// closing streams
oos.close();
fos.close();
System.out.println("Object has been serialized");
/* De-Serializing B's(subclass) object */
// Reading the object from a file
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
// Method for de-serialization of B's class object
B b2 = (B)ois.readObject();
// closing streams
ois.close();
fis.close();
System.out.println("Object has been deserialized");
System.out.println("i = " + b2.i);
System.out.println("j = " + b2.j);
}
}
Output:
i = 10
j = 20
Object has been serialized
Object has been deserialized
i = 10
j = 20

Case 2: If a superclass is not serializable, then subclass can still be serialized
Even though the superclass doesn't implement a Serializable interface, we can serialize subclass objects if the subclass itself implements a Serializable interface. So we can say that to serialize subclass objects, superclass need not be serializable. But what happens with the instances of superclass during serialization in this case. The following procedure explains this.
Case 2(a): What happens when a class is serializable, but its superclass is not?
Serialization: At the time of serialization, if any instance variable inherits from the non-serializable superclass, then JVM ignores the original value of that instance variable and saves the default value to the file.
De- Serialization: At the time of de-serialization, if any non-serializable superclass is present, then JVM will execute instance control flow in the superclass. To execute instance control flow in a class, JVM will always invoke the default(no-arg) constructor of that class. So every non-serializable superclass must necessarily contain a default constructor. Otherwise, we will get a runtime exception.
Java
// Java program to demonstrate
// the case if superclass need
// not to be serializable
// while serializing subclass
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
// superclass A
// A class doesn't implement Serializable
// interface.
class A {
int i;
// parameterized constructor
public A(int i){
this.i = i;
}
// default constructor
// this constructor must be present
// otherwise we will get runtime exception
public A()
{
i = 50;
System.out.println("A's class constructor called");
}
}
// subclass B
// implementing Serializable interface
class B extends A implements Serializable {
int j;
// parameterized constructor
public B(int i, int j)
{
super(i);
this.j = j;
}
}
// Driver class
public class Test {
public static void main(String[] args) throws Exception
{
B b1 = new B(10, 20);
System.out.println("i = " + b1.i);
System.out.println("j = " + b1.j);
// Serializing B's(subclass) object
// Saving of object in a file
FileOutputStream fos
= new FileOutputStream("abc.ser");
ObjectOutputStream oos
= new ObjectOutputStream(fos);
// Method for serialization of B's class object
oos.writeObject(b1);
// closing streams
oos.close();
fos.close();
System.out.println("Object has been serialized");
// De-Serializing B's(subclass) object
// Reading the object from a file
FileInputStream fis
= new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
// Method for de-serialization of B's class object
B b2 = (B)ois.readObject();
// closing streams
ois.close();
fis.close();
System.out.println("Object has been deserialized");
System.out.println("i = " + b2.i);
System.out.println("j = " + b2.j);
}
}
Output:
i = 10
j = 20
Object has been serialized
A's class constructor called
Object has been deserialized
i = 50
j = 20

Case 3: If the superclass is serializable, but we don’t want the subclass to be serialized
There is no direct way to prevent sub-class from serialization in java. One possible way by which a programmer can achieve this is by implementing the writeObject() and readObject() methods in the subclass and needs to throw NotSerializableException from these methods. These methods are executed during serialization and de-serialization, respectively. By overriding these methods, we are just implementing our custom serialization.
Java
// Java program to demonstrate
// how to prevent
// subclass from serialization
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
// superclass A
// implementing Serializable interface
class A implements Serializable
{
int i;
// parameterized constructor
public A(int i)
{
this.i = i;
}
}
// subclass B
// B class doesn't implement Serializable
// interface.
class B extends A
{
int j;
// parameterized constructor
public B(int i,int j)
{
super(i);
this.j = j;
}
// By implementing writeObject method,
// we can prevent
// subclass from serialization
private void writeObject(ObjectOutputStream out) throws IOException
{
throw new NotSerializableException();
}
// By implementing readObject method,
// we can prevent
// subclass from de-serialization
private void readObject(ObjectInputStream in) throws IOException
{
throw new NotSerializableException();
}
}
// Driver class
public class Test
{
public static void main(String[] args)
throws Exception
{
B b1 = new B(10, 20);
System.out.println("i = " + b1.i);
System.out.println("j = " + b1.j);
// Serializing B's(subclass) object
//Saving of object in a file
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// Method for serialization of B's class object
oos.writeObject(b1);
// closing streams
oos.close();
fos.close();
System.out.println("Object has been serialized");
// De-Serializing B's(subclass) object
// Reading the object from a file
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
// Method for de-serialization of B's class object
B b2 = (B)ois.readObject();
// closing streams
ois.close();
fis.close();
System.out.println("Object has been deserialized");
System.out.println("i = " + b2.i);
System.out.println("j = " + b2.j);
}
}
Output:
i = 10
j = 20
Exception in thread "main" java.io.NotSerializableException
at B.writeObject(Test.java:44)
Similar Reads
Object Graph in Java Serialization
In Java, Serialization is the process of converting an object into a format that can be easily stored or transmitted like a byte stream. When we think of Serialization, we probably think that a single object is being serialized, but what happens if the object contains references to other objects? Wi
4 min read
Using final with Inheritance in Java
Prerequisite - Overriding in java, Inheritance final is a keyword in java used for restricting some functionalities. We can declare variables, methods, and classes with the final keyword. Using final with inheritance During inheritance, we must declare methods with the final keyword for which we are
4 min read
Serializable Interface in Java
The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods. Classes implement it if they want their instances to be Serialized or Deserialized. Serializ
2 min read
Serialization and Deserialization in Java with Example
In Java, serialization plays a very important role it's something that we use a lot in our real life, even if we do not always notice it. Serialization helps us to save the current state of an object so that we can use it further and share complex data between different systems. In this article, we
8 min read
What is the Need of Inheritance in Java?
Inheritance, as we have all heard is one of the most important features of Object-Oriented Programming Languages whether it is Java, C++, or any other OOP language. But what is the need for Inheritance? Why is it so an important concept Inheritance can be defined as a mechanism by which one object c
6 min read
Understanding Object Cloning in Java with Examples
In this article we discuss the Cloneable interface that indicates that a class has provided a safe clone() method. To understand what cloning means recall what happens when you make a copy of a variable holding an object reference. The original and the copy are references to the same object. This me
4 min read
Generalization and Specialization in Java
General class? Loosely speaking, a class which tells the main features but not the specific details. The classes situated at the top of the inheritance hierarchy can be said as General. Specific class? A class which is very particular and states the specific details. The classes situated at the bott
5 min read
Why Java doesn't support Multiple Inheritance?
Multiple Inheritance is a feature provided by OOPS, it helps us to create a class that can inherit the properties from more than one parent. Some of the programming languages like C++ can support multiple inheritance but Java can't support multiple inheritance. This design choice is rooted in variou
5 min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Output of Java program | Set 23 (Inheritance)
Prerequisite: Inheritance in Java 1) What is the output of the following program? Java public class A extends B { public static String sing() { return "fa"; } public static void main(String[] args) { A a = new A(); B b = new A(); System.out.println(a.sing() + " " + b.sing()); } }
3 min read