1)Serializable is a marker interface which doesn’t have any method where as Externalizable extends Serializable interface and it provides method : writeExternal() and readExternal()
2)In Serializable , we only need to implement serializable interface which in turn provides its own default serialization process.
In Externalizable,we require to override writeExternal() and readExternal() methods to start serialization process.
3)In Serializable interface constructor is not called during deserialization process where as constructor is called during deserialization process in externalizable interface.
4)We can customize default serialization process of Serializable interface by defining methods readObject() and writeObject() of ObjectInputStream Class whereas in Externalizable interface ,serialization process is completely customized as we require to override method writeExternal() and readExternal().
5)Serializable interface provides less control over serialization because it is not mandate to use readObject() and writeObject() method as Serializable is a marker interface which provides default serialization process .On other hand Externalizable provides full control on serialization process as it is mandatory to override writeExternal() and readExternal() methods.
6)Externalizable interface gives better performance as using default serialization process of serializable interface we cant do much except making some non-required fields as transient and static but with Externalizable interface we can have full control over Serialization process.
7)In Serializable interface , it is very tough to modify class structure as any modification may break the serialization where as in Externalizable it is easy to modify class structure as developer has complete control over serialization logic.
Default Constructor is not getting called during Deserialization process if we implement Serializable Interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; class StudentInformation implements Serializable { private static final long serialVersionUID = 1L; private Integer rollNo; private String name; public StudentInformation() { System.out.println("No argument constructor is called"); } public StudentInformation(Integer rollNo,String name) { System.out.println("Parameterised constructor is called"); this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "Student Information is rollNo : " + rollNo + "and Student name is : "+name ; } } public class SerializableDemo { public static void main(String[] args) { StudentInformation object1 = new StudentInformation(21,"Shobhna"); try { OutputStream fout = new FileOutputStream("ser.txt"); ObjectOutput oout = new ObjectOutputStream(fout); System.out.println("Serializing StudentInformation object"); oout.writeObject(object1); fout.close(); oout.close(); System.out.println("Serialization process completed."); InputStream inputStream=new FileInputStream("ser.txt"); ObjectInput outInput=new ObjectInputStream(inputStream); System.out.println(""); System.out.println("DeSerializing StudentInformation objects"); StudentInformation std=(StudentInformation)outInput.readObject(); System.out.println(std); inputStream.close(); outInput.close(); System.out.println("Object DeSerialization completed."); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } |
OutPut :
Parameterised constructor is called
Serializing StudentInformation object
Serialization process completed.
Deserializing StudentInformation objects
Student Information is rollNo : 21and Student name is : Shobhna
Object Deserialization completed.
Default Constructor is getting called during Deserialization process if we implement Externalizable Interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import java.io.Externalizable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; class StudentDetail implements Externalizable { private static final long serialVersionUID = 1L; private Integer rollNo; private String name; public StudentDetail() { System.out.println("No argument constructor is called"); } public StudentDetail(Integer rollNo,String name) { System.out.println("Parameterised constructor is called"); this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "Student Information is rollNo : " + rollNo + "and Student name is : "+name ; } @Override public void writeExternal(ObjectOutput oo) throws IOException { oo.writeInt(rollNo); oo.writeObject(name); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.rollNo=in.readInt(); this.name=(String) in.readObject(); } } public class ExternalizableDemo { public static void main(String[] args) { StudentDetail studentDetail = new StudentDetail(21,"Shobhna"); try { OutputStream fout = new FileOutputStream("ser.txt"); ObjectOutput oout = new ObjectOutputStream(fout); System.out.println("Deserializating StudentDetail object"); oout.writeObject(studentDetail); fout.close(); oout.close(); System.out.println("Serialization completed."); //DeSerialization process > InputStream fin=new FileInputStream("ser.txt"); ObjectInput oin=new ObjectInputStream(fin); System.out.println("\nDeSerializating StudentDetail object"); StudentDetail stDetail=(StudentDetail)oin.readObject(); System.out.println(stDetail); fin.close(); oin.close(); System.out.println("DeSerialization completed."); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } |
OutPut:
Parameterised constructor is called
Deserializating StudentDetail object
Serialization completed.
DeSerializating StudentDetail object
No argument constructor is called
Student Information is rollNo : 21and Student name is : Shobhna
DeSerialization completed.
Please have a look at the posts on Serializable and Externalizable.
Leave a Reply