Java Serialization interview questions and answers

Serialization is one of most important concept in java. If you are going to face core java interview, then you might be asked some questions from Serialization.

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

Similarly ObjectInputStream has

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:

Create serializationMain.java as below:

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

Now Create a classed called DeserializeMain.java as below:

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

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.
Constructor called during Deserialization
Default constructor is not called during Deserialization process.
Default constructor is called during Deserialization process.

You may also like:

Related Posts

  • 22 May

    Java interview questions

    I have written a lot of java interview questions and this is index posts for all java interview questions. I will keep adding more interview questions to this post. Core Java Core java interview questions This list includes top 50 core java interview questions. Whether you are fresher or experienced programmer, this interview questions will […]

  • 09 November

    SOLID Principles in Java

    In this post, we will see 5 SOLID Principles in Java. Robert C. Martin gave five objected oriented design principles, and the acronym S.O.L.I.D is used for it. Each letter of this acronym talks about principles in Java. When you use all the principles of S.O.L.I.D in a combined manner, it becomes easier for you […]

  • 08 November

    Java interview questions for 2 years experience

    In this post, we are going to see Java interview questions for 2 to 3 years experienced. These are the most asked interview questions for freshers(0-3 years experienced). This question list will help you to crack java interview. I have already shared detailed answer over here before, you might find it helpful as well. I […]

  • 08 November

    Java interview questions for 5 years experience

    In this post, we will see Java interview questions for 5 to 6 years experience. When you have 5 years of experience as java developer, you need to have a good understanding of collections, multithreading concepts. If you are looking for the below queries then this post will help you as well. Java interview questions […]

  • 11 September

    Java technical interview questions

    In this post, we will see top Java technical interview asked in the Core Java interview.I have covered a lot of interview question on Java programming. Here is list of other java interview question you might find useful. Core java interview questions Java Collections interview questions Java String interview questions OOPs interview questions in java […]

  • 06 August

    Top 15 Spring Boot Interview questions

    In this post, we will see top 15 Spring Boot interview questions with answers.If you want to read more about Spring boot, you can go through Spring boot tutorial. If you are looking for below queries then this post will help you as well. Spring boot interview questions for 3 years experience Spring boot interview […]

Comments

  1. Hi Arpit,

    Can you please explain the last point with example in difference between Externalizable and Serializable interfaces

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.