Is constructor of super class called during DeSerialization process of sub class in java


It is depends on whether our superclass has implemented Serializable or not.

If superclass has implemented Serializable - constructor is not called during DeSerialization process.

If superclass has not implemented Serializable - constructor is called during DeSerialization process.




Full Program/SourceCode to show that If superclass has implemented Serializable - constructor is not called during DeSerialization process.

package SerDeser9SuperConsCheck;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
/*Author : AnkitMittal  Copyright- contents must not be reproduced in any form*/
class Super implements Serializable{
   private static final long serialVersionUID = 1L;
   public Super(){
          System.out.println("No-arg constructor of Super class");
   }
}
class Sub extends Super {  //it automatically implements Serializable (because it's subclass implements Serializable).
  
   private static final long serialVersionUID = 1L;
   private Integer id;
  
   public Sub(){
          System.out.println("No-arg constructor of sub class");
   }
  
   public Sub(Integer id) {
          System.out.println("1-arg constructor sub class");
          this.id = id;
   }
   @Override
   public String toString() {
          return "Employee [id=" + id + "]";
   }
}
public class SerializeDeser {
   public static void main(String[] args) {
          Sub object1 = new Sub(8);
          try {
                 OutputStream fout = new FileOutputStream("ser.txt");
                 ObjectOutput oout = new ObjectOutputStream(fout);
                 System.out.println("Serialization process has started, serializing objects...");
                 oout.writeObject(object1);
                 fout.close();
        oout.close();
                 System.out.println("Object Serialization completed.");
                
                 //DeSerialization process >
                
                
                 InputStream fin=new FileInputStream("ser.txt");
                 ObjectInput oin=new ObjectInputStream(fin);
                 System.out.println("\nDeSerialization process has started, displaying objects...");
                 Sub subObj=(Sub)oin.readObject();
                 System.out.println(subObj);
                 fin.close();
       oin.close();
       System.out.println("Object DeSerialization completed.");
                
                
          } catch (IOException | ClassNotFoundException  e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
No-arg constructor of Super class
1-arg constructor sub class
Serialization process has started, serializing objects...
Object Serialization completed.
DeSerialization process has started, displaying objects...
Employee [id=8]
Object DeSerialization completed.
*/
If we note output, superclass has implemented Serializable and its constructor is not called during DeSerialization process.



Full Program/SourceCode to show that If superclass has not implemented Serializable - constructor is called during DeSerialization process.
>
package SerDeser9SuperConsCheck;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
/*Author : AnkitMittal  Copyright- contents must not be reproduced in any form*/
class Super {
   public Super(){
          System.out.println("No-arg constructor of Super class");
   }
}
class Sub extends Super implements Serializable{    //it automatically implements Serializable (because it's subclass implements Serializable).
  
   private static final long serialVersionUID = 1L;
   private Integer id;
  
   public Sub(){
          System.out.println("No-arg constructor of sub class");
   }
  
   public Sub(Integer id) {
          System.out.println("1-arg constructor sub class");
          this.id = id;
   }
   @Override
   public String toString() {
          return "Employee [id=" + id + "]";
   }
}
public class SerializeDeser {
   public static void main(String[] args) {
          Sub object1 = new Sub(8);
          try {
                 OutputStream fout = new FileOutputStream("ser.txt");
                 ObjectOutput oout = new ObjectOutputStream(fout);
                 System.out.println("Serialization process has started, serializing objects...");
                 oout.writeObject(object1);
                 fout.close();
        oout.close();
                 System.out.println("Object Serialization completed.");
                
                 //DeSerialization process >
                
                
                 InputStream fin=new FileInputStream("ser.txt");
                 ObjectInput oin=new ObjectInputStream(fin);
                 System.out.println("\nDeSerialization process has started, displaying objects...");
                 Sub subObj=(Sub)oin.readObject();
                 System.out.println(subObj);
                 fin.close();
       oin.close();
       System.out.println("Object DeSerialization completed.");
                
                
          } catch (IOException | ClassNotFoundException  e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
No-arg constructor of Super class
1-arg constructor sub class
Serialization process has started, serializing objects...
Object Serialization completed.
DeSerialization process has started, displaying objects...
No-arg constructor of Super class
Employee [id=8]
Object DeSerialization completed.
*/
If we note output, superclass has not implemented Serializable and its constructor is called during DeSerialization process.





RELATED LINKS>

Serialization And Deserialization Tutorial


Customizing  Serialization process >



Constructor call during DeSerialization >


Interviews >


No comments:

Post a Comment