Java Serialization is the process of turning an object state in memory into a stream of bytes so we can do stuff like store it on disk or send it over the network.
Deserialization is the reverse process of Serialization where you can turn a stream of bytes into an object state in memory.
Code Snippet to persist Object state(Serialize) into disk
Code Snippet to deserialize object(Restoring the state) from disk
What is serialVersionUID?
When an object is serialize then with each serializable class a version number, called a serialVersionUID is associated, which is further used during deserialization
to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
If the receiver application has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's application class,
then deserialization will result in an InvalidClassException.
A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" static , final and long type.
If the class which is being serialized does not explicitly declare a serialVersionUID, then the serialization runtime will compute a default serialVersionUID
value for that class based on various attribtes of the class like attributes names, access specifier, data types of the variables etc.
However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values,
since the default serialVersionUID generation is very sensitive to class details that may vary depending on compiler implementations,
and can thus result in unexpected InvalidClassExceptions during deserialization process.
Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value.
It is also very important that serialVersionUID should be private, because serialVersionUID fields are not useful as inherited members.
If a class implements Serializable interface then it's all attributes must implements Serializable interface
As per java doc, Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility
of the class to save and restore the contents of its instances.
The writeExternal and readExternal methods of the Externalizable
interface are implemented by a class to give the class complete
control over the format and contents of the stream for an object
and its supertypes. These methods must explicitly
coordinate with the supertype to save its state. These methods supersede
customized implementations of writeObject and readObject methods.
When super class does not implements Serializable interface
Here we have two classes Frog and SpecialFrog. Frog is super class which does not implements Serializable interface and subclass SpecialFrog
which implements Serializable interface. If you serialize an instance of SpecialFrog then only state of subclass SpecialFrog will be serialized , not super class Frog
since it does not implements Serializable interface.
Please refer the below picture:-
Solution of above problem:-
in above picture inside SpecialFrog class we have added one special method "private void writeObject()" to serialize all the attributes of super class Frog which does not implements
Serializable interface, in this case attribute are "name" & "color".
When super class implements Serializable interface
Here we have two classes Frog and SpecialFrog. Frog is super class which implements Serializable interface and subclass SpecialFrog
does not implements Serializable interface. As we know that Serializable feature is inherited. So SpecialFrog also now becomes Serializable. But we do not want to make SpecialFrog Serilizable. if someone try to Serialize SpecialFrog then it should throw the exception.
Please refer the below picture to implement it:-