Question 1: What is Serialization?

Answer:
Java provides mechanism called serialization to persists java objects in a form of ordered or sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.So if we need to serialize any object then it can be read and deserialize it using object’s type and other information so we can retrieve original object.Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.
ObjectOutputStream has many method for serializing object but commonly used method is

Question 2: What is need of Serialization?

Answer:
Serialization is usually used when there is need to send your data over network or to store in files. By data I mean objects and not text.

Now the problem is your Network infrastructure and your Hard disk is hardware components that understand bits and bytes but not Java objects.

Serialization is the translation of Java object’s values/states to bytes to send it over network or to save it.On the other hand, Deserialization is conversion of byte code to corresponding java objects.

Question 3: Can you explain about Concept of serialVersionUID?

Answer:
serialVersionUID is used to ensure that same class(That was used during Serialization) is loaded during Deserialization.serialVersionUID is used for version control of object.You can read more at serialVersionUID in java serialization

Question 4: Is it necessary to implement Serializable interface if you want to serialize any object?

Answer:
Yes, it is necessary to implement Serializable interface if you want to serialize any object. Serializable is marker interface.Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.

Question 5: Can you Serialize static variables?

Answer :
No,you can’t.As you know static variable are at class level not at object level and you serialize a object so you can’t serialize static variables.

Question 6: How can you customize serialization process?

Answer :
You can customize Serialization process by defining writeObject and readObject method.Java serialization provides a mechanism such that if you have private methods with particular signature then they will get called during serialization and deserialization, so in this way, we can customize Serialization process.
For example:

One thing should be kept in mind that ObjectInputStream should read data in same sequence in which we have written data to ObjectOutputStream.

Question 7: How can you avoid certain member variable of class to be serialized?

Answer:
You can mark that variable as either static or transient. Let’s see a simple example using transient variable.Transient variable is the variable whose value is not serialized during serialization. You will get default value for these variable when you deserialize it.

Let’s say you have Country class and you don’t want to Serialize population attribute as it will change with time, so you can declare population attribute as transient and it won’t serialized any more.

Transient keyword example:

Create a classed called Country.java as below:

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

packageorg.arpit.java2blog;

import java.io.Serializable;

publicclassCountryimplementsSerializable{

Stringname;

transient longpopulation;

publicCountry(){

super();

}

publicCountry(Stringname,longpopulation){

super();

this.name=name;

this.population=population;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publiclonggetPopulation(){

returnpopulation;

}

publicvoidsetPopulation(longpopulation){

this.population=population;

}

}

Create serializationMain.java as below:

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

packageorg.arpit.java2blog;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

publicclassSerializeMain{

/**

* @author Arpit Mandliya

*/

publicstaticvoidmain(String[]args){

Country india=newCountry();

india.setName("India");

india.setPopulation(100000);

try

{

FileOutputStream fileOut=newFileOutputStream("country.ser");

ObjectOutputStream outStream=newObjectOutputStream(fileOut);

outStream.writeObject(india);

outStream.close();

fileOut.close();

}catch(IOExceptioni)

{

i.printStackTrace();

}

System.out.println("serialized");

}

}

When you run above program, you will get below output:

1

2

3

serialized

Now Create a classed called DeserializeMain.java as below:

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

packageorg.arpit.java2blog;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

publicclassDeserializeMain{

/**

* @author Arpit Mandliya

*/

publicstaticvoidmain(String[]args){

Country india=null;

try

{

FileInputStream fileIn=newFileInputStream("country.ser");

ObjectInputStream in=newObjectInputStream(fileIn);

india=(Country)in.readObject();

in.close();

fileIn.close();

}catch(IOExceptioni)

{

i.printStackTrace();

return;

}catch(ClassNotFoundExceptionc)

{

System.out.println("Country class not found");

c.printStackTrace();

return;

}

System.out.println("Deserialized Country...");

System.out.println("Country Name : "+india.getName());

System.out.println("Population : "+india.getPopulation());

}

}

When you run above program, you will get below output:

1

2

3

4

5

Deserialized Country...

Country Name:India

Population:0

As you can see in above example, we have declared population as transient, so after deserialization, its value became 0 (Default value for long)

Question 8: What if superclass is Serializable? Does that mean child class is automatically Serializable?

Answer : Yes

Question 9: What if superclass is Serializable but you don’t want subclass to be Serializable?

Answer : If you don’t want subclass to serializable then you need to implement writeObject() and readObject() method and need to throw NotSerializableException from this methods.

Question 10 :What is externalizable interface?

Answer: As name suggest it is externalilizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshalling and unmarshalling of objects.Externalizable interface extends Serializable interface. If you implement this interface then you need to override following methods.

Question 11 : What are differences between Serializable and Externalizable in Java?

Answer :

Parameter

Serializable

Externalizable

Marker interface

It is marker interface. You don’t have to provide implementation of any method.

Externalizable is not marker interface, you have to override writeExternal and readExternal method.

Control

Serializable interface has less control over serialization process and it is optional to override readObject and writeObject.

Externalizable interface has more control over serialization process and it is mandatory to override writeExternal and readExternal.

Performance

JVM uses reflection to perform serialization in the case of Serializable interface which is quite slow.

Programmer have to implement readExternal and writeExternal methods but it relatively results in better performance

Supersedes

NA

If you implement Externalizable interface and provide implementation of readExternal and writeExternal then it supersedes readObject and writeObject methods in that class. It is due to the fact that Externalizable extends Serializable interface.