Difference Between Serializable and Externalizable in Java Serialization
The process of writing the state of an object to a file is called serialization, but strictly speaking, it is the process of converting an object from java supported form into a file supported form or network supported form. By using fileOutputStream and objectOutputStream classes we can implement serialization.
But we can serialize only serializable objects. An object is said to be serializable if and only if the corresponding class implements a Serializable interface. Serializable interface present in java.io package, and it doesn’t contain any method and hence it is a marked interface. If we are trying to serializable a non-serializable object then we will get Runtime Exception saying notSerializableException.
Object has been serialized Object has been deserialized Name:Ram Age:34 2364
In serialization, everything is taken care of by JVM and the programmer doesn’t have any control. In serialization, it is always possible to solve the total object to file, and it is not possible to save part of the object which may create performance problems. To overcome this problem we should go for externalization.
The main advantage over serialization is everything is taken care of by the programmer and JVM doesn’t have any control. Based on our requirements we can save either the total object or part of the object which improves the performance of the system. To provide the generalizable ability for any java object compulsory the corresponding class should implement a generalizable interface.
This interface defines two methods as follows:
public void writeExternal( ObjectIOutput obj ) throws IOException
This method will be executed automatically at the time of serialization within this method we have to write code to save the required variable to the file.
public void readExternal(ObjectInput in )throws IOException , ClassNotFoundException
This method will be executed automatically at the time of deserialization. Within this method, we have to write code to read the required variable from the file and assign it to the current object. But strictly speaking at the time of deserialization JVM will create a separate new object by executing a public no-argument constructor and then on that object, JVM will call the readExternal method.
Hence, every Externalizable implemented class should compulsorily contain a public no-argument constructor otherwise we will get Runtime Exception saying InvalidClassException.
Public no-argument constructor Name : Ram Age : 35
Now, we are done discussing both of them, let us conclude all the differences in a table for a better reach of understanding.
Serializable Externalizable A serializable interface is used to implement serialization. An externalizable interface used to implement Externalization Serializable is a marker interface i.e. it does not contain any method. The externalizable interface is not a marker interface and thus it defines two methods writeExternal() and readExternal(). Serializable interface passes the responsibility of serialization to JVM and the programmer has no control over serialization, and it is a default algorithm. The externalizable interface provides all serialization responsibilities to a programmer and hence JVM has no control over serialization. Serialization using a serializable interface has bad performance. Serialization using an externalizable interface has better performance. Default serialization does not require any no-arg constructor. A public no-arg constructor is required while using an Externalizable interface. It is hard to analyze and modify class structure because any change in structure may break serialization. It is relatively easy to analyze and modify class structure because of complete control over serialization logic. Using a serializable interface we save the total object to a file, and it is not possible to save part of the object. Base on our requirements we can save either the total object or part of the object. Transient keyword play an important role here. Transient keyword won’t play any role.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.