serialization Introduction
Serialization: The Process of Saving an object to a file is called “serialization”. But strictly
speaking serialization is the process of converting an object from java supported format to network or file supported.
By using FileOutPutStream, ObjectOutPutStream classes we can achieve serialization
Deserialization: The process of reading an object from a file is called deserialization. But strictly speaking it is the process of Converting an object from network supported format or file supported format to java supported format.
By using FileInputStream, ObjectInputStream we can achieve deserialization.
Ex:-
class Dog implements Serializable
{
int i= 10;
int j= 20;
}
class serializedemo
{
public static void main(String arg[])
{
Dog d = new Dog();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog)ois.readObject();
System.out.println(d1.i+"---"+d2.j);
}
}
Customized Serialization
During default Serialization there may be a chance of loss of information because of transient variables.
Example:
import java.io.*;
class Dog implements Serializable
{
transient Cat c = new Cat();
}
class Cat
{
int j= 20;
}
class SerializeDemo
{
public static void main(String arg[])throws Exception
{
Dog d = new Dog();
System.out.println("Before Serialization:"+d.c.j);
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog)ois.readObject();
System.out.println(d1.c.j);
}
}
Internal Process of Diagrammatic Form
In the above program before serialization dog object tell ‘j’ value. But after deserialization dog object can’t tell j value(d1.c.j) will rise NullPointerException.
i.e during Serialization there may be a chance of loss of information. To recover this information we should customize Serialization process. which is nothing but “Customized Serialization”
We can implement customized Serialization by using the following two methods.
We can implement customized Serialization by using the following two methods.
private void writeObject(OutputStream os)throws Exception
{
….
….
….
}
This method will be executed automatically by the JVM at the time of Serialization.
private void readObject(InputStream is)throws Exception
{
….
….
….
}
This method will be executed automatically by the JVM at the time of deSerialization.
Ex:-
import java.io.*;
class Dog implements Serializable
{
transient Cat c = new Cat();
private void writeObject(ObjectOutputStream os)throws IOException
{
int x = c.j;
os.writeInt(x);
}
private void readObject(ObjectInputStream is)throws IOException,
ClassNotFoundException
{
is.defaultReadObject();
c=null
d
d
j=20
c
Cat
c=null
d
int k = is.readInt();
c = new Cat();
c.j = k;
}
}
class Cat
{
int j= 20;
}
class SerializeDemo
{
public static void main(String arg[])throws Exception
{
Dog d = new Dog();
System.out.println("Before Serialization:"+d.c.j);
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog)ois.readObject();
System.out.println(d1.c.j);
}
}
O/P:- 20
Inheritance in Serialization
If the parent class is Serializable bydefault all the child classes also Serializable. i.e Serializable nature is inherited from parent to child.
Ex:-
import java.io.*;
class Animal implements Serializable
{
int i = 10;
}
class Dog extends Animal
{
int j = 20;
}
class SerializeDemo
{
public static void main(String arg[])throws Exception
{
Dog d = new Dog();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog)ois.readObject();
System.out.println(d1.i+"-----"+d1.j);
}
}
If the child class is Serializable and some of the parent classes are not Serializable, Still we are allowed to serialize child class Objects. While performing serialization JVM ignores the inherited variables which are coming from non-serializable parents.
While performing de-serialization JVM will check is there any parent class is non-serializable or not.
If any parent is non-serializable JVM will create an object for every non-serializable parent and share it’s instance variables for the current child object.
The non-serializable parent class should compulsory contain no-argument constructor other wise we will get
runtime error.
Ex:-
import java.io.*;
class Animal
{
int i = 10;
Animal()
{
System.out.println("Animal Constructor");
}
}
class Dog extends Animal implements Serializable
{
int j = 20;
Dog()
{
System.out.println("Dog Constructor");
}
}
class SerializeDemo
{
public static void main(String arg[])throws Exception
{
Dog d = new Dog();
d.i = 888;
d.j = 999;
System.out.println(d.i+"-----"+d.j);
System.out.println("Serilization Started");
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
System.out.println("Deserialization Started");
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog)ois.readObject();
System.out.println(d1.i+"-----"+d1.j);
}
}
Output:
No comments:
Post a Comment