Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 30 November 2013

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’.


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\beingjavaguys\core\Human.java

package com.beingjavaguys.core;

import java.io.Serializable;

/**
 * @author Nagesh Chauhan
 */
public class Human implements Serializable {

 private static final long serialVersionUID = 1L;

 String gender;
 String color;

 Human(String gender, String color) {
  this.gender = gender;
  this.color = color;
 }

 public String getGender() {
  return gender;
 }

 public void setGender(String gender) {
  this.gender = gender;
 }

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }

}


\src\com\beingjavaguys\core\Man.java

package com.beingjavaguys.core;

/**
 * @author Nagesh Chauhan
 */
public class Man extends Human {

 private String name;
 private String address;

 Man(String gender, String color, String name, String address) {
  super(gender, color);
  this.name = name;
  this.address = address;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

}


\src\com\beingjavaguys\core\Implementation.java

package com.beingjavaguys.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Nagesh Chauhan
 */
public class Implementation {
 public static void main(String args[]) {

  /*
   * Assigning values to Man class's instance
   */
  Man man = new Man("Male", "Black", "Anderw", "Delhi");

  /*
   * Serializing Man's instance
   */
  try {
   FileOutputStream fileOutputStream = new FileOutputStream(
     "serialObject.ser");
   ObjectOutputStream objectOutputStream = new ObjectOutputStream(
     fileOutputStream);
   objectOutputStream.writeObject(man);

  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException ioe) {
   // TODO Auto-generated catch block
   ioe.printStackTrace();
  }

  /*
   * Deserializing Man's instance
   */
  Man manout = null;

  try {
   FileInputStream fileInputStream = new FileInputStream(
     "serialobject.ser");
   ObjectInputStream inputStream = new ObjectInputStream(
     fileInputStream);
   manout = (Man) inputStream.readObject();

  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException ioe) {
   // TODO Auto-generated catch block
   ioe.printStackTrace();
  } catch (ClassNotFoundException cnf) {
   // TODO Auto-generated catch block
   cnf.printStackTrace();
  }

  /*
   * Printing values from deserialized Man's object
   */
  System.out.println("Printing value of Deserailized Man's instance :");
  System.out.println("Gender: " + manout.getGender());
  System.out.println("Color: " + manout.getColor());
  System.out.println("Name: " + manout.getName());
  System.out.println("Address: " + manout.getAddress());

 }

}

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\beingjavaguys\core\Human.java

package com.beingjavaguys.core;

/**
 * @author Nagesh Chauhan
 */
public class Human {
 String gender;
 String color;

 Human() {
 }

 Human(String gender, String color) {
  this.gender = gender;
  this.color = color;
 }

 public String getGender() {
  return gender;
 }

 public void setGender(String gender) {
  this.gender = gender;
 }

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }

}


\src\com\beingjavaguys\core\Man.java

package com.beingjavaguys.core;

import java.io.Serializable;

/**
 * @author Nagesh Chauhan
 */
public class Man extends Human implements Serializable {

 private static final long serialVersionUID = 1L;
 private String name;
 private String address;

 Man(String gender, String color, String name, String address) {
  super(gender, color);
  this.name = name;
  this.address = address;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

}


\src\com\beingjavaguys\core\Implementation.java

package com.beingjavaguys.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Nagesh Chauhan
 */
public class Implementation {
 public static void main(String args[]) {

  /*
   * Assigning values to Man class's instance
   */
  Man man = new Man("Male", "Black", "Anderw", "Delhi");

  /*
   * Serializing Man's instance
   */
  try {
   FileOutputStream fileOutputStream = new FileOutputStream(
     "serialObject.ser");
   ObjectOutputStream objectOutputStream = new ObjectOutputStream(
     fileOutputStream);
   objectOutputStream.writeObject(man);

  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException ioe) {
   // TODO Auto-generated catch block
   ioe.printStackTrace();
  }

  /*
   * Deserializing Man's instance
   */
  Man manout = null;

  try {
   FileInputStream fileInputStream = new FileInputStream(
     "serialobject.ser");
   ObjectInputStream inputStream = new ObjectInputStream(
     fileInputStream);
   manout = (Man) inputStream.readObject();

  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException ioe) {
   // TODO Auto-generated catch block
   ioe.printStackTrace();
  } catch (ClassNotFoundException cnf) {
   // TODO Auto-generated catch block
   cnf.printStackTrace();
  }

  /*
   * Printing values from deserialized Man's object
   */
  System.out.println("Printing value of Deserailized Man's instance :");
  System.out.println("Gender: " + manout.getGender());
  System.out.println("Color: " + manout.getColor());
  System.out.println("Name: " + manout.getName());
  System.out.println("Address: " + manout.getAddress());

 }

}


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.








Thanks for reading !
Being Java Guys Team




1 comment:

  1. these program hellps to understand the implementation
    www.besanttechnologies.com

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft