Set - 9

Question 41 :

What is serialization?

Answer :

Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.


Question 42 :

How do I serialize an object to a file?

Answer :

The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.


Question 43 :

Which methods of Serializable interface should I implement?

Answer :

The serializable interface is an empty interface, it does not contain any methods. So we do not implement any methods.


Question 44 :

How can I customize the serialization process? i.e. how can one have a control over the serialization process?

Answer :

Yes it is possible to have control over serialization process. The class should implement Externalizable interface. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.


Question 45 :

What is the common usage of serialization?

Answer :

Whenever an object is to be sent over the network, objects need to be serialized. Moreover if the state of an object is to be saved, objects need to be serilized.