Object Serialization with Inheritance in Java

In this section, we are going to talk about serialization with the help of code. So, Serialization is referred to as a process in which an object is converted into a stream of bytes or an array of bytes. The byte array can be the class, version, and the internal state of the object. And, Deserialization is referred to as the rebuilding of a byte stream into a Java Object that must be usable. Let’s understand Serialization with respect to inheritance :

Case 1:When superclass executes a Serializable interface and the subclass does not.

In this case, the objects of the subclass get serialize directly due to serialization of the superclass, irrespective of subclass implements, or not implements the Serializable interface.

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;

class superclass implements Serializable
{ 
    int a; 
    
    public superclass(int a)  
    { 
        this.a = a; 
    } 
}
class subclass extends superclass  
{ 
    int b; 
    public subclass(int a, int b)  
    { 
        super(a); 
        this.b = b; 
    } 
}

public class ObjectSerialisation {

  public static void main(String[] args) throws Exception {
    subclass b1 = new subclass(10,20); 
        
    System.out.println("a is " + b1.a); 
    System.out.println("b is " + b1.b); 
        
    FileOutputStream fileoutputstream= new FileOutputStream("abc.ser"); 
    ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream); 
              
        
    objectoutputstream.writeObject(b1); 
        
    objectoutputstream.close(); 
    fileoutputstream.close(); 
              
    System.out.println("Object has been serialized"); 
          
    FileInputStream fileinputstream = new FileInputStream("abc.ser"); 
    ObjectInputStream objectinputstream = new ObjectInputStream(fileinputstream); 
              
         
    subclass b2 = (subclass)objectinputstream.readObject(); 
        
    objectinputstream.close(); 
    fileinputstream.close(); 
              
    System.out.println("Object has been deserialized"); 
          
    System.out.println("a is " + b2.a); 
    System.out.println("b is " + b2.b); 
    
  }
  
}

Output is:

a is 10
b is 20
Object has been serialized
Object has been deserialized
a is 10
b is 20

Case 2: When the superclass is not implementing the Serializable interface and subclass implements.

During serialization, any variable belonging to the non-serializable superclass, the value of this variable is ignored by JVM and the default value is stored in the file. And during de-serialization, JVM will execute instance control flow in the superclass. For this purpose, JVM will call the default constructor of the superclass. So every non-serializable superclass must contain default constructor, otherwise, we will get a runtime error. Let’s analyze both of the cases.

2 a) The superclass has a constructor:

class superclass 
{ 
    int a; 
    
    public superclass(int a)  
    { 
        this.a = a; 
    } 
    superclass(){
    	a=5;
    	System.out.println("Class A constructor is called");
    }
}
class subclass extends superclass implements Serializable
{ 
    int b; 
    public subclass(int a, int b)  
    { 
        super(a); 
        this.b = b; 
    } 
}

public class ObjectSerialisation {

  public static void main(String[] args) throws Exception {
    subclass b1 = new subclass(10,20); 
        
    System.out.println("a is " + b1.a); 
    System.out.println("b is " + b1.b); 
        
    FileOutputStream fileoutputstream= new FileOutputStream("abc.ser"); 
    ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream); 
              
        
    objectoutputstream.writeObject(b1); 
        
    objectoutputstream.close(); 
    fileoutputstream.close(); 
              
    System.out.println("Object has been serialized"); 
          
    FileInputStream fileinputstream = new FileInputStream("abc.ser"); 
    ObjectInputStream objectinputstream = new ObjectInputStream(fileinputstream); 
              
         
    subclass b2 = (subclass)objectinputstream.readObject(); 
        
    objectinputstream.close(); 
    fileinputstream.close(); 
              
    System.out.println("Object has been deserialized"); 
          
    System.out.println("a is " + b2.a); 
    System.out.println("b is " + b2.b); 
    
  }
  
}

Output :

a is 10
b is 20
Object has been serialized
Class A constructor is called
Object has been deserialized
a is 5
b is 20

2b. The superclass does not have a constructor:

class superclass 
{ 
    int a; 
    
    public superclass(int a)  
    { 
        this.a = a; 
    } 
    
}
class subclass extends superclass implements Serializable
{ 
    int b; 
    public subclass(int a, int b)  
    { 
        super(a); 
        this.b = b; 
    } 
}

public class ObjectSerialisation {

  public static void main(String[] args) throws Exception {
    subclass b1 = new subclass(10,20); 
        
    System.out.println("a is " + b1.a); 
    System.out.println("b is " + b1.b); 
        
    FileOutputStream fileoutputstream= new FileOutputStream("abc.ser"); 
    ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream); 
              
    objectoutputstream.writeObject(b1); 
        
    objectoutputstream.close(); 
    fileoutputstream.close(); 
              
    System.out.println("Object has been serialized"); 
          
    FileInputStream fileinputstream = new FileInputStream("abc.ser"); 
    ObjectInputStream objectinputstream = new ObjectInputStream(fileinputstream); 
              
         
    subclass b2 = (subclass)objectinputstream.readObject(); 
        
    objectinputstream.close(); 
    fileinputstream.close(); 
              
    System.out.println("Object has been deserialized"); 
          
    System.out.println("a is " + b2.a); 
    System.out.println("b is " + b2.b); 
    
  }
  
}

Output :

a is 10
b is 20
Object has been serialized
Exception in thread “main” java.io.InvalidClassException: subclass; no valid constructor

Case 3:When we need to serialize superclass but not to the subclass.

This can be done by implementing the writeObject() during serialization and readObject()  during deserialization methods in the subclass and needs to throw NotSerializableException from these methods. So, we are actually overriding these methods to make custom serialization.

class superclass implements Serializable
{ 
    int a; 
    
    public superclass(int a)  
    { 
        this.a = a; 
    } 
    
}
class subclass extends superclass
{ 
    int b; 
    public subclass(int a, int b)  
    { 
        super(a); 
        this.b = b; 
    } 
    private void writeObject(ObjectOutputStream out) throws IOException { 
       throw new NotSerializableException(); 
    } 
       
  private void readObject(ObjectInputStream in) throws IOException { 
       throw new NotSerializableException(); 
    } 
}

public class ObjectSerialisation {

  public static void main(String[] args) throws Exception {
    subclass b1 = new subclass(10,20); 
        
    System.out.println("a is " + b1.a); 
    System.out.println("b is " + b1.b); 
        
    FileOutputStream fileoutputstream= new FileOutputStream("abc.ser"); 
    ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream); 
              
    objectoutputstream.writeObject(b1); 
    objectoutputstream.close(); 
    fileoutputstream.close(); 
        
    System.out.println("Object has been serialized");   
        
    FileInputStream fileinputstream = new FileInputStream("abc.ser"); 
    ObjectInputStream objectinputstream = new ObjectInputStream(fileinputstream); 
    subclass b2 = (subclass)objectinputstream.readObject(); 
    objectinputstream.close(); 
        
    fileinputstream.close();       
    System.out.println("Object has been deserialized"); 
    System.out.println("a is " + b2.a); 
    System.out.println("b is " + b2.b); 
    
  }
  
  
}

Output :

a is 10
b is 20
Exception in thread “main” java.io.NotSerializableException

 

Leave a Reply