Search This Blog

Wednesday, May 22, 2019

How Serialization works in Java(A brief introduction)

Inheritance in Java Serialization - Example and Explanation

In our previous discussions we came across, Serialization, serialVersionUID and transient variable. In this particular blog we will see ?Inheritance in Serialization?.

Add caption

In case of inheritance, when we want to serialize an object there may be three possible scenarios. 

(1) If Super class is Serializable

In case super class is Serializable than all its subclasses will be serializable by default. No need to implement serializable interface in subclass explicitly. See the implementation below. 

\src\com\javanjvm\core\Human.java

  1. package com.javanjvm.core;    
  2.     
  3. import java.io.Serializable;    
  4.     
  5. /**  
  6.  * @author bharat 
  7.  */    
  8. public class Human implements Serializable {    
  9.     
  10.  private static final long serialVersionUID = 1L;    
  11.     
  12.  String gender;    
  13.  String color;    
  14.     
  15.  Human(String gender, String color) {    
  16.   this.gender = gender;    
  17.   this.color = color;    
  18.  }    
  19.     
  20.  public String getGender() {    
  21.   return gender;    
  22.  }    
  23.     
  24.  public void setGender(String gender) {    
  25.   this.gender = gender;    
  26.  }    
  27.     
  28.  public String getColor() {    
  29.   return color;    
  30.  }    
  31.     
  32.  public void setColor(String color) {    
  33.   this.color = color;    
  34.  }    
  35.     
  36. }    


\src\com\javanjvm\core\Man.java

  1. package com.javanjvm.core;    
  2.     
  3. /**  
  4.  * @author bharat 
  5.  */    
  6. public class Man extends Human {    
  7.     
  8.  private String name;    
  9.  private String address;    
  10.     
  11.  Man(String gender, String color, String name, String address) {    
  12.   super(gender, color);    
  13.   this.name = name;    
  14.   this.address = address;    
  15.  }    
  16.     
  17.  public String getName() {    
  18.   return name;    
  19.  }    
  20.     
  21.  public void setName(String name) {    
  22.   this.name = name;    
  23.  }    
  24.     
  25.  public String getAddress() {    
  26.   return address;    
  27.  }    
  28.     
  29.  public void setAddress(String address) {    
  30.   this.address = address;    
  31.  }    
  32.     


\src\com\javanjvm\core\Implementation.java

  1. package com.javanjvm.core;    
  2.     
  3. import java.io.FileInputStream;    
  4. import java.io.FileNotFoundException;    
  5. import java.io.FileOutputStream;    
  6. import java.io.IOException;    
  7. import java.io.ObjectInputStream;    
  8. import java.io.ObjectOutputStream;    
  9.     
  10. /**  
  11.  * @author bharat 
  12.  */    
  13. public class Implementation {    
  14.  public static void main(String args[]) {    
  15.     
  16.   /*  
  17.    * Assigning values to Man class's instance  
  18.    */    
  19.   Man man = new Man("Male""Black""Anderw""Delhi");    
  20.     
  21.   /*  
  22.    * Serializing Man's instance  
  23.    */    
  24.   try {    
  25.    FileOutputStream fileOutputStream = new FileOutputStream(    
  26.      "serialObject.ser");    
  27.    ObjectOutputStream objectOutputStream = new ObjectOutputStream(    
  28.      fileOutputStream);    
  29.    objectOutputStream.writeObject(man);    
  30.     
  31.   } catch (FileNotFoundException e) {    
  32.    // TODO Auto-generated catch block    
  33.    e.printStackTrace();    
  34.   } catch (IOException ioe) {    
  35.    // TODO Auto-generated catch block    
  36.    ioe.printStackTrace();    
  37.   }    
  38.     
  39.   /*  
  40.    * Deserializing Man's instance  
  41.    */    
  42.   Man manout = null;    
  43.     
  44.   try {    
  45.    FileInputStream fileInputStream = new FileInputStream(    
  46.      "serialobject.ser");    
  47.    ObjectInputStream inputStream = new ObjectInputStream(    
  48.      fileInputStream);    
  49.    manout = (Man) inputStream.readObject();    
  50.     
  51.   } catch (FileNotFoundException e) {    
  52.    // TODO Auto-generated catch block    
  53.    e.printStackTrace();    
  54.   } catch (IOException ioe) {    
  55.    // TODO Auto-generated catch block    
  56.    ioe.printStackTrace();    
  57.   } catch (ClassNotFoundException cnf) {    
  58.    // TODO Auto-generated catch block    
  59.    cnf.printStackTrace();    
  60.   }    
  61.     
  62.   /*  
  63.    * Printing values from deserialized Man's object  
  64.    */    
  65.   System.out.println("Printing value of Deserailized Man's instance :");    
  66.   System.out.println("Gender: " + manout.getGender());    
  67.   System.out.println("Color: " + manout.getColor());    
  68.   System.out.println("Name: " + manout.getName());    
  69.   System.out.println("Address: " + manout.getAddress());    
  70.     
  71.  }    
  72.     
  73. }    

Output:Here is expected output, printing values for deserialized object.



(2) If Super class is not Serializable but subclass is

In case super class is not Serializable than to serialize the subclass?s object we must implement serializable interface in subclass explicitly. In this case the superclass must have a no-argument constructor in it. See the implementation below. 

\src\com\javanjvm\core\Human.java

  1. package com.javanjvm.core;    
  2.     
  3. /**  
  4.  * @author bharat 
  5.  */    
  6. public class Human {    
  7.  String gender;    
  8.  String color;    
  9.     
  10.  Human() {    
  11.  }    
  12.     
  13.  Human(String gender, String color) {    
  14.   this.gender = gender;    
  15.   this.color = color;    
  16.  }    
  17.     
  18.  public String getGender() {    
  19.   return gender;    
  20.  }    
  21.     
  22.  public void setGender(String gender) {    
  23.   this.gender = gender;    
  24.  }    
  25.     
  26.  public String getColor() {    
  27.   return color;    
  28.  }    
  29.     
  30.  public void setColor(String color) {    
  31.   this.color = color;    
  32.  }    
  33.     
  34. }    


\src\com\javanjvm\core\Man.java

  1. package com.javanjvm.core;    
  2.     
  3. import java.io.Serializable;    
  4.     
  5. /**  
  6.  * @author Nagesh Chauhan  
  7.  */    
  8. public class Man extends Human implements Serializable {    
  9.     
  10.  private static final long serialVersionUID = 1L;    
  11.  private String name;    
  12.  private String address;    
  13.     
  14.  Man(String gender, String color, String name, String address) {    
  15.   super(gender, color);    
  16.   this.name = name;    
  17.   this.address = address;    
  18.  }    
  19.     
  20.  public String getName() {    
  21.   return name;    
  22.  }    
  23.     
  24.  public void setName(String name) {    
  25.   this.name = name;    
  26.  }    
  27.     
  28.  public String getAddress() {    
  29.   return address;    
  30.  }    
  31.     
  32.  public void setAddress(String address) {    
  33.   this.address = address;    
  34.  }    
  35.     
  36. }    


\src\com\javanjvm\core\Implementation.java

  1. package com.javanjvm.core;    
  2.     
  3. import java.io.FileInputStream;    
  4. import java.io.FileNotFoundException;    
  5. import java.io.FileOutputStream;    
  6. import java.io.IOException;    
  7. import java.io.ObjectInputStream;    
  8. import java.io.ObjectOutputStream;    
  9.     
  10. /**  
  11.  * @author bharat  
  12.  */    
  13. public class Implementation {    
  14.  public static void main(String args[]) {    
  15.     
  16.   /*  
  17.    * Assigning values to Man class's instance  
  18.    */    
  19.   Man man = new Man("Male""Black""Anderw""Delhi");    
  20.     
  21.   /*  
  22.    * Serializing Man's instance  
  23.    */    
  24.   try {    
  25.    FileOutputStream fileOutputStream = new FileOutputStream(    
  26.      "serialObject.ser");    
  27.    ObjectOutputStream objectOutputStream = new ObjectOutputStream(    
  28.      fileOutputStream);    
  29.    objectOutputStream.writeObject(man);    
  30.     
  31.   } catch (FileNotFoundException e) {    
  32.    // TODO Auto-generated catch block    
  33.    e.printStackTrace();    
  34.   } catch (IOException ioe) {    
  35.    // TODO Auto-generated catch block    
  36.    ioe.printStackTrace();    
  37.   }    
  38.     
  39.   /*  
  40.    * Deserializing Man's instance  
  41.    */    
  42.   Man manout = null;    
  43.     
  44.   try {    
  45.    FileInputStream fileInputStream = new FileInputStream(    
  46.      "serialobject.ser");    
  47.    ObjectInputStream inputStream = new ObjectInputStream(    
  48.      fileInputStream);    
  49.    manout = (Man) inputStream.readObject();    
  50.     
  51.   } catch (FileNotFoundException e) {    
  52.    // TODO Auto-generated catch block    
  53.    e.printStackTrace();    
  54.   } catch (IOException ioe) {    
  55.    // TODO Auto-generated catch block    
  56.    ioe.printStackTrace();    
  57.   } catch (ClassNotFoundException cnf) {    
  58.    // TODO Auto-generated catch block    
  59.    cnf.printStackTrace();    
  60.   }    
  61.     
  62.   /*  
  63.    * Printing values from deserialized Man's object  
  64.    */    
  65.   System.out.println("Printing value of Deserailized Man's instance :");    
  66.   System.out.println("Gender: " + manout.getGender());    
  67.   System.out.println("Color: " + manout.getColor());    
  68.   System.out.println("Name: " + manout.getName());    
  69.   System.out.println("Address: " + manout.getAddress());    
  70.     
  71.  }    
  72.     
  73. }    


Output:Here is expected output, printing values for deserialized object.



If superclass is not Serializable then all values of the instance variables inherited from super class will be initialized by calling constructor of Non-Serializable Super class during deserialization process. As we can see in the output figure above ?Gender? gets a default value and ?Color? is getting null because of no default value set. 


(3) If the superclass is serializable but we don?t want the subclass to be serialized.

To prevent subclass from being serialized we must implement writeObject() and readObject() method and need to throw NotSerializableException from these methods. 

Here we are done with 'Inheritance in Java Serialization'. In our upcoming blogs we will see more about Java Programming and other opensource technologies.

No comments :

Post a Comment