Open In App

Object Serialization with Inheritance in Java

Last Updated : 06 Nov, 2025
Comments
Improve
Suggest changes
37 Likes
Like
Report

Object Serialization in Java allows you to save (serialize) and restore (deserialize) the state of an object, even when it’s part of an inheritance hierarchy. When dealing with inheritance, the serialization behavior depends on whether the superclass and subclass implement the Serializable interface.

  • Serialization: It 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: It 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.

Understanding Serialization with Inheritance

Serialization behavior changes depending on whether the superclass or subclass implements the Serializable interface. There are three main cases to understand:

Case 1: Superclass is Serializable -> Subclass is Automatically Serializable

If the superclass implements Serializable, then its subclasses are automatically serializable, even if they don’t explicitly implement the interface.

case_1
Serialization with Inheritance
Java
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; 
    } 
} 

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); 
        
        //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"); 
        
        // 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: 

output
output

Explanation: Since class A implements Serializable, the subclass B is also serializable automatically. Both i and j retain their values after deserialization.

Case 2: If a superclass is not serializable, then subclass can still be serialized

Even if the superclass doesn’t implement Serializable, a subclass can still be serialized if it implements the Serializable interface. However, instance variables from the non-serializable superclass are not saved during serialization

Note: The non-serializable superclass must have a no-argument constructor, because it will be called during deserialization to reinitialize its part of the object.

case_2
Serialization with Inheritance
Java
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

        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

        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

Explanation: A is not serializable, so its field i is not saved. During deserialization, A’s default constructor is invoked, setting i = 50. The field j (from B) is restored correctly.

Case 3: If the superclass is serializable, but we don’t want the subclass to be serialized

If the superclass implements Serializable, there’s no direct way to stop a subclass from being serialized. However, we can manually block serialization by overriding the following methods in the subclass:

  • writeObject(ObjectOutputStream out)
  • readObject(ObjectInputStream in)

Throw a NotSerializableException inside these methods to prevent serialization and deserialization.

Java
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(); 
    } 
} 

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 
        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 
        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: 

output
Output

Explanation: Here, the subclass B overrides writeObject() and readObject() to block serialization. This approach effectively prevents subclass objects from being serialized or deserialized, even though their superclass is serializable.

Suggested Quiz
3 Questions

What is serialization in Java?

  • A

    Converting an object into a class

  • B

    Converting an object’s state into a byte stream

  • C

    Converting byte stream into an object

  • D

    Storing methods of an object

Explanation:

Serialization converts an object’s state into a byte stream so it can be stored or transferred.

If a superclass implements Serializable, what happens to its subclass?

  • A

    The subclass must also implement Serializable

  • B

    The subclass cannot be serialized

  • C

    The subclass is automatically serializable

  • D

    Only superclass fields are serialized

Explanation:

When a superclass is serializable, all its subclasses automatically become serializable.

What happens to superclass variables if the superclass is NOT serializable but the subclass is serializable?

  • A

    They are serialized normally

  • B

    They are ignored and set to default values

  • C

    They are restored using the superclass no-arg constructor

  • D

    Serialization fails with an exception


Explanation:

During deserialization, the non-serializable superclass constructor is called to initialize its variables.

Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Article Tags :

Explore