Serialization OverViewAgenda :
Serialization: (1.1 v)
Diagram: ![]() De-Serialization:
Diagram: ![]() Example 1: import java.io.*; class Dog implements Serializable { int i=10; int j=20; } class SerializableDemo { public static void main(String args[])throws Exception{ Dog d1=new Dog(); System.out.println("Serialization started"); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(d1); System.out.println("Serialization ended"); System.out.println("Deserialization started"); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog)ois.readObject(); System.out.println("Deserialization ended"); System.out.println(d2.i+"................"+d2.j); } } Output: Serialization started Serialization ended Deserialization started Deserialization ended 10................20 Diagram: ![]() Note:
Example2: import java.io.*; class Dog implements Serializable { int i=10; int j=20; } class Cat implements Serializable { int i=30; int j=40; } class SerializableDemo { public static void main(String args[])throws Exception{ Dog d1=new Dog(); Cat c1=new Cat(); System.out.println("Serialization started"); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(d1); oos.writeObject(c1); System.out.println("Serialization ended"); System.out.println("Deserialization started"); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog)ois.readObject(); Cat c2=(Cat)ois.readObject(); System.out.println("Deserialization ended"); System.out.println(d2.i+"................"+d2.j); System.out.println(c2.i+"................"+c2.j); } } Output: Serialization started Serialization ended Deserialization started Deserialization ended 10................20 30................40 Transient keyword:
Static Vs Transient :
Transient Vs Final:
Example 3: import java.io.*; class Dog implements Serializable { static transient int i=10; final transient int j=20; } class SerializableDemo { public static void main(String args[])throws Exception{ Dog d1=new Dog(); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(d1); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog)ois.readObject(); System.out.println(d2.i+"................"+d2.j); } } Output: 10................20 Diagram: ![]() Table:
We can serialize any no of objects to the file but in which order we serialized in the same order only we have to deserialize. Example : Dog d1=new Dog( ); Cat c1=new Cat( ); Rat r1=new Rat( ); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(d1); oos.writeObject(c1); oos.writeObject(r1); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog)ois.readObject(); Cat c2=(Cat)ois.readObject(); Rat r2=(Rat)ois.readObject(); If we don't know order of objects : Example : FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Object o=ois.readObject( ); if(o instanceof Dog) { Dog d2=(Dog)o; //perform Dog specific functionality } else if(o instanceof Cat) { Cat c2=(Cat)o; //perform Cat specific functionality } . . . } Object graph in serialization:
Example 4: import java.io.*; class Dog implements Serializable { Cat c=new Cat(); } class Cat implements Serializable { Rat r=new Rat(); } class Rat implements Serializable { int j=20; } class SerializableDemo { public static void main(String args[])throws Exception{ Dog d1=new Dog(); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(d1); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog)ois.readObject(); System.out.println(d2.c.r.j); } } Output: 20 Diagram: ![]()
Customized serialization:During default Serialization there may be a chance of lose of information due to transient keyword.(Ex : mango ,money , box) Example 5: import java.io.*; class Account implements Serializable { String userName="Bhaskar"; transient String pwd="kajal"; } class CustomizedSerializeDemo { public static void main(String[] args)throws Exception{ Account a1=new Account(); System.out.println(a1.userName+"........."+a1.pwd); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(a1); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Account a2=(Account)ois.readObject(); System.out.println(a2.userName+"........."+a2.pwd); } } Output: Bhaskar.........kajal Bhaskar.........null Diagram: ![]()
Demo program for customized serialization to recover loss of information which is happen due to transient keyword. import java.io.*; class Account implements Serializable { String userName="Bhaskar"; transient String pwd="kajal"; private void writeObject(ObjectOutputStream os)throws Exception { os.defaultWriteObject(); String epwd="123"+pwd; os.writeObject(epwd); } private void readObject(ObjectInputStream is)throws Exception{ is.defaultReadObject(); String epwd=(String)is.readObject(); pwd=epwd.substring(3); } } class CustomizedSerializeDemo { public static void main(String[] args)throws Exception{ Account a1=new Account(); System.out.println(a1.userName+"........."+a1.pwd); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(a1); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Account a2=(Account)ois.readObject(); System.out.println(a2.userName+"........."+a2.pwd); } } Output: Bhaskar.........kajal Bhaskar.........kajal Diagram: ![]() At the time of Account object serialization JVM will check is there any writeObject() method in Account class or not. If it is not available then JVM is responsible to perform serialization(default serialization). If Account class contains writeObject() method then JVM feels very happy and executes that Account class writeObject() method. The same rule is applicable for readObject() method also. Serialization with respect to inheritance :Case 1:If parent class implements Serializable then automatically every child class by default implements Serializable. That is Serializable nature is inheriting from parent to child. Hence even though child class doesn't implements Serializable , we can serialize child class object if parent class implements serializable interface. Example 7: import java.io.*; class Animal implements Serializable { int i=10; } class Dog extends Animal { int j=20; } class SerializableWRTInheritance { public static void main(String[] args)throws Exception{ Dog d1=new Dog(); System.out.println(d1.i+"........"+d1.j); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(d1); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog)ois.readObject(); System.out.println(d2.i+"........"+d2.j); } } Output: 10........20 10........20 Even though Dog class does not implements Serializable interface explicitly but we can Serialize Dog object because its parent class animal already implements Serializable interface. Note : Object class doesn't implement Serializable interface. Case 2:
Example 8: 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 SerializableWRTInheritance { public static void main(String[] args)throws Exception{ Dog d1=new Dog(); d1.i=888; d1.j=999; FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(d1); System.out.println("Deserialization started"); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog)ois.readObject(); System.out.println(d2.i+"........."+d2.j); } } Output: Animal constructor called Dog constructor called Deserialization started Animal constructor called 10.........999 Diagram: ![]() Externalization : ( 1.1 v )
Externalizable interface defines 2 methods :
public void writeExternal(ObjectOutput out) throws IOExceptionThis method will be executed automaticcay at the time of Serialization with in this method , we have to write code to save required variables to the file . public void readExternal(ObjectInput in) throws IOException , ClassNotFoundExceptionThis method will be executed automatically at the time of deserialization with in this method , we have to write code to save read required variable from file and assign to the current object At the time of deserialization Jvm will create a seperate new object by executing public no-arg constructor on that object JVM will call readExternal() method. Every Externalizable class should compusory contains public no-arg constructor otherwise we will get RuntimeExcepion saying "InvaidClassException" . Example : import java.io.*; class ExternalDemo implements Externalizable { String s ; int i ; int j ; public ExternalDemo() { System.out.println("public no-arg constructor"); } public ExternalDemo(String s , int i, int j) { this.s=s ; this.i=i ; this.j=j ; } public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(s); out.writeInt(i); } public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { s=(String)in.readObject(); i= in.readInt(); } } public class Externalizable1 { public static void main(String[] args)throws Exception { ExternalDemo t1=new ExternalDemo("ashok", 10, 20); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(t1); FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); ExternalDemo t2=(ExternalDemo)ois.readObject(); System.out.println(t2.s+"-------"+t2.i+"--------"+t2.j); } } output : public no-arg constructor ashok -------- 10 ------ 0 Diagram : ![]()
Difference between Serialization & Externalization :
serialVersionUID :To perform Serialization & Deserialization internally JVM will use a unique identifier , which is nothing but serialVersionUID . At the time of serialization JVM will save serialVersionUID with object. At the time of Deserialization JVM will compare serialVersionUID and if it is matched then only object will be Deserialized otherwise we will get RuntimeException saying "InvalidClassException". The process in depending on default serialVersionUID are :
We can solve above problems by configuring our own serialVersionUID . we can configure serialVersionUID as follows :private static final long serialVersionUID = 1L;Example : class Dog implements Serializable { private static final long serialVersionUID=1L; int i=10; int j=20; } class Sender { public static void main(String[] args) throws Exception { Dog d1=new Dog(); FileOutputStream fos=new FileOutputStream("abc.ser"); ObjectOutputStream oos= new ObjectOutputStream(fos); oos.writeObject(d1); } } class Receiver { public static void main(String[] args)throws Exception { FileInputStream fis=new FileInputStream("abc.ser"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d2=(Dog) ois.readObject(); System.out.println(d2.i+"-----"+d2.j); } } In the above program after serialization even though if we perform any change to Dog.class file , we can deserialize object. We if configure our own serialVersionUID both sender and receiver not required to maintain the same JVM versions. Note : some IDE's generate explicit serialVersionUID. |
BACK |