Java Serialization: Class 8

4) SERIALIZATION WITH RESPECT TO INHERITANCE:

Case a: Parent class implements Serializable but child class do not
If parent is Serializable then by default child is Serializable. That is Serializable nature in inheritance from Parent to Child. Hence even though child doesn't implements Serializable, if parent class implements Serializable then we can Serialize child Class Object.



import java.io.*;

class Animal implements Serializable{
 int i=10;
}

class Dog extends Animal{
 int j=20;
}

class SerializeDemo{
 public static void main(String[]args){
  Dog d1 = new Dog();
  System.out.println(d1.i +" "+d1.j);
  FileOutputStream fos = new FileOutputStream("xyz.ser");
  ObjectOutputStream oos = new ObjectOutputStream(fos);
  oos.writeobject(d1);

  FileInputStream fis = new FileInputStream("xyz.ser");
  ObjectInputStream ois = new ObjectInputStream(fis);
  Dog d2 = (Dog)ois.readObject();
  System.out.println(d1.i+" "+d1.j);
 }
}

It is clear from above image, that the j variable from Dog (child class) is also getting serialized in .ser file because we have our parent class implements serializable.

Case b: Child class implements Serializable but parent class do not
i) Even though parent class doesn't implements Serializable interface we can serialize Child Class Object if Child class implements Serializable Interface i.e to Serialize Child class Object, Parent class need not to be Serializable.

ii) At the time of Serialization, JVM will check that is any instance variable is inheriting from Non.-Serializable parent or not. If any variable from Non-Serializable parent then JVM ignores original value and save default value to the file.

iii) At the time of DeSerialization JVM will check is any parent class in non Serializable or not. If any parent class is non -Serializable then JVM will execute Instance control Flow in the non-serializable parent and share it instance variables value to the current.

iv) To execute instance Control flow execution of Non-serializable parent JVM will always invoke no argument constructor. hence every non-serializable class should compulsory contain no-argument Constructor. Otherwise we will get runtime exception saying InvalidClassException.



import java.io.*;

class Animal{
 int i=10;
 Animal(){
  System.out.println("Animal Constructor Called");
 }
}

class Dog extends Animal implements Serializable{
  int j=20;
  Dog(){
    System.out.println("Dog Constructor called");
  }
}

class SerializeDemo{
  public static void main(String[]args){
    Dog d1= new Dog();
    d1.i=234;
    d1.j=123;

    FileOutputStream fos = new FileOutputStream("xyz.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(d1);
    Syste

    FileInputStream fis = new FileInputStream("xyz.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Dog d2 = (Dog) ois.readObject();

    Syst.out.println("d2.i + " " + d2.j);
  }
}
OUTPUT:
Animal Constructor Called
Dog Constructor called
Animal Constructor Called
10 123

In case b, it shows the effect of instance control flow. As we can see, that variable i is from parent class and it's default value is 10. so when we try to serialize it, then default value of i will  get written in .ser  file and during deserialize, the value will be 0 and then overwrite by default value of i. This whole process is instance control flow that play it's role when parent is not serializable and we try to serialize it's variable.

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment