Serialization Part 2

 Java Serialization with Inheritance (IS-A Relationship)

If a class implements Serializable interface then all its sub classes will also be serializable. Let's see the example given below:

SerializeISA.java

import java.io.Serializable;    

class Person implements Serializable{    

 int id;    

 String name;    

 Person(int id, String name) {    

  this.id = id;    

  this.name = name;    

 }    

}    

class Student extends Person{    

 String course;    

 int fee;    

 public Student(int id, String name, String course, int fee) {    

  super(id,name);    

  this.course=course;    

  this.fee=fee;    

 }    

}    

public class SerializeISA  

{    

 public static void main(String args[])  

 {    

    try{    

  //Creating the object    

  Student s1 =new Student(211,"ravi","Engineering",50000);    

  //Creating stream and writing the object    

  FileOutputStream fout=new FileOutputStream("f.txt");    

  ObjectOutputStream out=new ObjectOutputStream(fout);    

  out.writeObject(s1);    

  out.flush();    

  //closing the stream    

  out.close();    

  System.out.println("success");    

  }catch(Exception e){System.out.println(e);}    

  try{    

  //Creating stream to read the object    

  ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));    

  Student s=(Student)in.readObject();    

  //printing the data of the serialized object    

  System.out.println(s.id+" "+s.name+" "+s.course+" "+s.fee);    

  //closing the stream    

  in.close();    

  }catch(Exception e){System.out.println(e);}    

 }    

}  

The SerializeISA class has serialized the Student class object that extends the Person class which is Serializable. Parent class properties are inherited to subclasses so if parent class is Serializable, subclass would also be.

Java Serialization with Aggregation (HAS-A Relationship)

If a class has a reference to another class, all the references must be Serializable otherwise serialization process will not be performed. In such case, NotSerializableException is thrown at runtime.

Address.java
class Address{    
 String addressLine,city,state;    
 public Address(String addressLine, String city, String state) {    
  this.addressLine=addressLine;    
  this.city=city;    
  this.state=state;    
 }    
}   

Student.java
import java.io.Serializable;  
public class Student implements Serializable{  
 int id;  
 String name;  
 Address address;//HAS-A  
 public Student(int id, String name) {  
  this.id = id;  
  this.name = name;  
 }  
}  
Since Address is not Serializable, you cannot serialize the instance of the Student class.

Java Serialization with the static data member

If there is any static data member in a class, it will not be serialized because static is the part of class not object.

Java Serialization with array or collection

Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed.

Externalizable in java

The Externalizable interface provides the facility of writing the state of an object into a byte stream in compress format. It is not a marker interface.

The Externalizable interface provides two methods:

  • public void writeExternal(ObjectOutput out) throws IOException
  • public void readExternal(ObjectInput in) throws IOException

Java Transient Keyword

If you don't want to serialize any data member of a class, you can mark it as transient.

Employee.java

class Employee implements Serializable{  

 transient int id;  

 String name;  

 public Student(int id, String name) {  

  this.id = id;  

  this.name = name;  

 }  

}  

Now, id will not be serialized, so when you deserialize the object after serialization, you will not get the value of id. It will return default value always. In such case, it will return 0 because the data type of id is an integer.

Comments

Popular posts from this blog

Java 8 : Find the number starts with 1 from a list of integers

Junit Mockito and Power Mockito

Find Loop in a Linked List