Table of Contents
- Java Serialization Tutorial:
- Question 1: What is Serialization?
- Question 2: What is need of Serialization?
- Question 3: Can you explain about Concept of serialVersionUID?
- Question 4: Is it necessary to implement Serializable interface if you want to serialize any object?
- Question 5: Can you Serialize static variables?
- Question 6: How can you customize serialization process?
- Question 7: How can you avoid certain member variable of class to be serialized?
- Question 8: What if superclass is Serializable?  Does that mean child class is automatically Serializable?
- Question 9:Â What if superclass is Serializable but you don’t want subclass to be Serializable?
- Question 10 :What is externalizable interface?
- Question 11 : What are differences between Serializable and Externalizable in Java?
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.
Java Serialization Tutorial:
- Serialization in java
- Java Serialization interview questions and answers
- serialversionuid in java serialization
- externalizable in java
- Transient keyword in java
- Difference between Serializable and Externalizable in Java
Here are list of questions that may be asked on Serialization.
Question 1: What is Serialization?
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
1 2 3 4 5 6 |
 private void writeObject(ObjectOutputStream os) throws IOException { } |
1 2 3 4 5 6 |
   private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException    {          } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private void writeObject(ObjectOutputStream os) throws IOException, ClassNotFoundException { try { os.defaultWriteObject(); os.writeInt(address.getHomeNo()); os.writeObject(address.getStreet()); os.writeObject(address.getCity()); } catch (Exception e) { e.printStackTrace(); } } private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException { try { is.defaultReadObject(); int homeNo=is.readInt(); String street=(String) is.readObject(); String city=(String) is.readObject(); address=new Address(homeNo,street,city); } |
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.
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 |
package org.arpit.java2blog; import java.io.Serializable; public class Country implements Serializable { String name; transient long population; public Country() { super(); } public Country(String name, long population) { super(); this.name = name; this.population = population; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } } |
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 |
package org.arpit.java2blog; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class SerializeMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Country india = new Country(); india.setName("India"); india.setPopulation(100000); try { FileOutputStream fileOut = new FileOutputStream("country.ser"); ObjectOutputStream outStream = new ObjectOutputStream(fileOut); outStream.writeObject(india); outStream.close(); fileOut.close(); }catch(IOException i) { i.printStackTrace(); } System.out.println("serialized"); } } |
1 2 3 |
serialized |
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 |
package org.arpit.java2blog; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializeMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Country india = null; try { FileInputStream fileIn =new FileInputStream("country.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); india = (Country) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { 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()); } } |
1 2 3 4 5 |
Deserialized Country... Country Name : India Population : 0 |
Question 8: What if superclass is Serializable?  Does that mean child class is automatically Serializable?
Question 9:Â What if superclass is Serializable but you don’t want subclass to be Serializable?
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](https://java2blog.com/externalizable-in-java/ “Externalizable”) interface extends Serializable interface. If you implement this interface then you need to override following methods.
1 2 3 4 5 6 7 8 9 10 11 |
@Override public void readExternal(ObjectInput arg0) throws IOException,ClassNotFoundException { } @Override public void writeExternal(ObjectOutput arg0) throws IOException { } |
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.
|
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:
- Core java interview questions
- Java Collections interview questions
- Java String interview questions
- OOPs interview questions in java
- Java Multithreading interview questions
- Exceptional handling interview questions in java
- Method overloading and overriding interview questions
- web services interview questions
- restful web services interview questions
- Data structure and algorithm Interview Questions
- Spring interview questions
- Hibernate interview questions
Hi Arpit,
Can you please explain the last point with example in difference between Externalizable and Serializable interfaces