Professional Documents
Culture Documents
Serialization
Serialization
Serialization
// Once object have been transferred over network or persisted in file or in DB,
we could de-serialized the object and retain its state as it was serialized.
De-Serialization:
I have one object and I want to write the object to the file, binary data.
FileOutputStream by using oos.write(Object obj);
Diagram:
FIS, OIS
Example 1:
readObject(): It is used to read bytes from the persistence and to create object
from those bytes and it’s return an object which needs to be type cast to correct
type.
Example 2:
#Transient Keyword:
Note:
Static VS Transient:
1. Static variable is not part of object state hence they won't participate
in serialization because of this declaring a static variable as transient
there is no use. Serialization applicable on object.
2. If it is participating JVM will ignore original value and store default
value.
(Static variable created at the time of class loading.
//j will be created at the time of class loading. Static is related to class, No way
related to object.
10 ---> Coming from the file,
20 ---> coming from class level method area.
Example 3:
O/P: 1 ------ 10
Table:
Note:
We can serialize any no of objects to the file but in which order we serialized in the
same order only we have to de-serialize. If we don’t follow order then
ClassCastException.
Example:
If we don’t know order of the objects:
Example:
1. Whenever we are serializing an object the set of all objects which are
reachable from that object will be serialized automatically. This group of
objects is nothing but object graph in serialization.
// whenever we serialized d object, the set of all object which are reachable from
the object will be serialized automatically cat, rat object will be serialized because
dog have Cat, Cat have the Rat, Rat have j so J=20.
In the above example whenever we are serializing Dog object
automatically Cat and Rat objects will be serialized because these are
part of object graph of Dog object.
Among Dog, Cat, Rat if at least one object is not serializable then we
will get runtime exception saying "NotSerializableException".
Diagram:
#Customized Serialization:
Example:
Diagram:
In the above example before serialization Account object can provide
proper username and password. But after Deserialization Account object
can provide only username bur not password. This is due to declaring
password as transient. Hence doing default serialization there may be a
chance of loss of information due to transient keyword.
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.
Example:
Even though Dog class does not implement 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. If serializable then all
class in java implements serializable, if the serializable, then all class in java
implements serializable.
Case 2:
Diagram:
#Externalization:
1. writeExternal( )
2. readExternal( )
Example:
1. If the class implements Externalizable interface then only part of the
object will be saved in the case output is
SerialVersionUID:
2. Both sender and receiver should use the same version of JVM if there any
incompatibility in JVM versions then receive enable to deserializable
because of different serialVersionUID , in this case receiver will get
RuntimeException saying "InvalidClassException" .
3. To generate serialVersionUID internally JVM will use complex Algorithm
which may create performance problems.
/* At the time of serialization with every object, sender side JVM will save one
Unique ID, at the time of de-Serialization receives side JVM will compare that
object UNIQUE ID with local UNIQUE ID, if both are match, de-serialization will be
happen if not match ‘InvalidClassException’.
// suppose, we have a class which you serialized it and stored in persistence and
later modified that class to add a new field. What will happen if you de-
serialization the object already serialization.
Ans: It’s dependent on whether class has its own serialVersion ID or not.
Example:
Diagram:
In the above program after serialization even though if we perform any change to
Dog.class file, we can de-serialize object.
At the time of serialization with every object, sender side JVM will save
one unique ID, at the time of de-serialization receives side JVM will
compare that object with unique ID with local class unique ID, if both
are match, de-serialization will be happen. If not match
“InvalidClassException”.
Suppose, we have a class which you serialized it and stored in persistent and later
modified that class to add a new field. What will happen if you de-serialized the
object already serialized.
Ans: It’s depends on whether class has its, own serial version ID or not.
Interview Questions
Q) While serializing you want some of the members not to serialize? How do you
achieve it?
Q) What will happen if one of the members in the class doesn’t implement
Serializable interface?
Q) If a class is serializable but its super class in not what will be the state of the
instance variables inherited from super class after de-Serialization.
Q) Can you customize Serialization process or can you override default Serialization
process in java?
Q) Suppose super class of a new class implement Serializable interface, how can
you avoid new class to being serialized?
Q) Suppose you have a class which you serialized it and stored in persistence and
later modified that class to add a new field. What will happen if you De-
Serialization the object already serialized?
Q) What are the compatible changes and incompatible changes in Java Serialization
Mechanism?
If you have serialized a class & then added few fields in it and then deserialize already
serialized version of class, how can you ensure that you don’t end up throwing
InvalidClassException?
>Simply we need to define serialVersionUID in class.
When we Deserialize class ( class which has been modified after Serialization and
also class doesn’t declare SerialVersionUID) InvalidClassException is thrown.
When we Deserialize class ( class which has been modified after Serialization and
also class declare SerialVersionUID) its gets DeSerialized successfully.
>We can can convert JSON to transfer the object. JSON is helpful in stringifying and
de stringifying object.
>Hibernate (ORM tool) helps in persisting object as it in database and later we can
read persisted object.
>We can convert object into XML (as done in web services) and transfer object over
network
Transient Variables:
Serialization is not applicable on transient variables (it helps in saving time and space
during Serialization process), we must mark all rarely used variables as transient.
We can initialize transient variables during deSerialization by customizing
deSerialization process
Q) What will happen if we have used List, Set and Map as member of class?
ArrayList, HashSet and HashMap implements Serializable interface, so if we will use
them as member of class they will get Serialized and DeSerialized as well.
We can simply use readResove() method to return same instance of class, rather
than creating a new one.
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
If you don't define serialVersionUID in your serilizable class, Java compiler will make
one by creating a hash code using most of your class attributes and features. When
an object gets serialized, this hash code is stamped on the object which is known as
the SerialVersionUID of that object. This ID is required for the version control of an
object. SerialVersionUID can be specified in the class file also. In case, this ID is not
specified by you, then Java compiler will regenerate a SerialVersionUID based on
updated class and it will not be possible for the already serialized class to recover
when a class field is added or modified. Its recommended that you always declare a
serialVersionUID in your Serializable classes.