«Back to Home

Core Java

Topics

Introduction Of Serialization In Java

Serialization
 
In Java, a process of writing the state of an object into stream of bytes refers to serialization. It is mostly used in Hibernate, RMI, JPA, EJB, JMS technologies and the reverse operation of serialization is called deserialization.
 
Java provides an interface (java.io.Serializable) to serialize an object. If we want any class to be serialized , the class needs to implement the given interface.
 
Serializable interface is a Marker Interface. Hence, there is no method in Serializable Interface.
 
By default, String class and all the wrapper classes implements java.io.Serializable interface.
 
Advantage of Serialization

This byte stream can be used for the different purposes like,
  • Write to Disk

  • Store in Memory

  • Sent byte stream to other platform over network

  • Save byte stream in DB(As BLOB)
java.io.Serializable interface

Serializable is a marker interface which has no body. It is just used as a marker interface that mark Java classes which support a certain capability. But It should be implemented by the class whose object we want to serialize.
 
Let's see an example, given below.
 
Code
  1. import java.io.Serializable;  
  2. public class Employee implements Serializable {  
  3.     int empid;  
  4.     String empname;  
  5.     int empsalary;  
  6.     public Employee(int empid, String empname, int empsalary) {  
  7.         this.empid = empid;  
  8.         this.empname = empname;  
  9.         this.empsalary = empsalary;  
  10.     }  
  11. }  
37

ObjectOutputStream class

The ObjectOutputStream class writes the primitive data types and Java objects to an OutputStream. The objects can be read, using an ObjectInputStream. Only objects that support the java.io.Serializable interface can be written to the streams.
 
Constructor

public ObjectOutputStream(OutputStream out) throws IOException {}

It creates an ObjectOutputStream, which writes to the specified OutputStream.
 
Methods

public final void writeObject(Object obj) throws IOException {}

This method is used to write the specified object to the ObjectOutputStream.
 
public void flush() throws IOException {}

This method is used to flush the current output stream.
 
public void close() throws IOException {}

This method is used to close the current output stream.
 
Example of serialization.
 
Code
  1. import java.io.*;  
  2. public class EmployeeTest {  
  3.     public static void main(String args[]) throws Exception {  
  4.         Employee e1 = new Employee(101"Harry"25000);  
  5.         Employee e2 = new Employee(136"Mia"30000);  
  6.         FileOutputStream fos = new FileOutputStream("Data.txt");  
  7.         ObjectOutputStream os = new ObjectOutputStream(fos);  
  8.         os.writeObject(e1);  
  9.         os.writeObject(e2);  
  10.         os.flush();  
  11.         os.close();  
  12.         fos.close();  
  13.         System.out.println("Run successfully....");  
  14.     }  
  15. }  
38

Output

39

In the example, shown above, we serialize the object of Employee class and writeObject() method of ObjectOutputStream class, which is used to serialize the object. We save the state of the object in the file named Data.txt.
 
Deserialization

In Java, a process of reconstructing the object from the serialized state refers to deserialization. It is the reverse operation of serialization.
 
ObjectInputStream class

An ObjectInputStream is used to deserialize the objects and primitive data written, using an ObjectOutputStream.
 
Constructor

public ObjectInputStream(InputStream in) throws IOException {}

It creates an ObjectInputStream, which reads from the specified InputStream.
 
Methods

public final Object readObject() throws IOException, ClassNotFoundException{}

This method is used to read an object from the input stream.
 
public void close() throws IOException {}
 
This method is used to close ObjectInputStream.
 
Example of Deserialization is given below.
 
Code
  1. import java.io.*;  
  2. public class EmployeeTest1 {  
  3.     public static void main(String args[]) throws Exception {  
  4.         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Data.txt"));  
  5.         Employee e = (Employee) ois.readObject();  
  6.         System.out.println(e.empid + " " + e.empname + " " + e.empsalary);  
  7.         ois.close();  
  8.     }  
  9. }  
40

Output

41
 
Serialization with Inheritance

When a class implements serializable then it’s all child classes will also be serializable.
 
Let's see an example, given below.
 
Code
  1. import java.io.Serializable;  
  2. public class Animal implements Serializable {  
  3.     String name;  
  4.     int age;  
  5.     Animal(String name, int age) {  
  6.         this.name = name;  
  7.         this.age = age;  
  8.     }  
  1. public class Dog extends Animal {  
  2.     String food;  
  3.     public Dog(String name, int age) {  
  4.         super(name, age);  
  5.         this.food = food;  
  6.     }  
  7. }  
42

43

In the example, shown above, we can serialize the Dog class object, which extends the Animal class that is serializable. Parent class properties are inherited to child classes. Thus, if parent class is Serializable, all child class would also be.
 
Serialization with Aggregation

When a class has a reference of another class, all the references must be serializable, else serialization process will not be performed and NotSerializableException is thrown at the runtime.
 
Let’s see an example, given below.
 
Code
  1. public class Address {  
  2.     String street, town, city, state;  
  3.     public Address(String street, String town, String city, String state) {  
  4.         this.street = street;  
  5.         this.town = town;  
  6.         this.city = city;  
  7.         this.state = state;  
  8.     }  
  9. }  
  1. import java.io.Serializable;  
  2. public class EmployeeEx implements Serializable {  
  3.     int empid;  
  4.     String empname;  
  5.     Address empaddress;  
  6.     public EmployeeEx(int id, String name) {  
  7.         this.empid = empid;  
  8.         this.empname = empname;  
  9.     }  
  10. }  
44

45
In the example, shown above, Address is not serializable and we cannot serialize the object of EmployeeEx class.
 
Serialization with static data member

Static is the part of a class and not an object due to which it will not be serialized in Java.
 
Let’s see an example, given below.
 
Code
  1. import java.io.Serializable;  
  2. class EmployeeEx implements Serializable {  
  3.     int empid;  
  4.     String empname;  
  5.     static String empCompany = "MNC company";  
  6.     public EmployeeEx(int id, String name) {  
  7.         this.empid = empid;  
  8.         this.empname = empname;  
  9.     }  
  10. }  
46

Serialization with array or collection

In Java, all the objects of an array or a collection must be serializable. If any object is not serializable, serialization will be unsuccessful.
 
Externalizable

The Externalizable interface writes the state of an object into a byte stream in the compressed format and It is not a Marker Interface.
 
Externalizable interface methods

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

Thus, we learnt a process of writing the state of an object into the stream of bytes, which refers to serialization and the reverse operation of serialization is called deserialization and we learnt, how to create it in Java.