Friday 13 May 2016

Serialization Interview Questions



Serialization in Java

  1. What is Serialization in Java?
    1. Object Serialization in Java is a process used to convert Object into a binary format which can be persisted into disk or sent over network to any other running Java virtual machine; the reverse process of creating object from binary stream is called deserialization in Java. Java provides Serialization API for serializing and deserializing object which includes java.io.Serializable, java.io.Externalizable, ObjectInputStream and ObjectOutputStream etc.
  2. How to make a Java class Serializable?
    1. Making a class Serializable in Java is very easy, Your Java class just needs to implements java.io.Serializable interface and JVM will take care of serializing object in default format.
  3. What is the difference between Serializable and Externalizable interface in Java?
    1. This is most frequently asked question in Java serialization interview. Here is my version Externalizable provides us writeExternal() and readExternal() method which gives us flexibility to control java serialization mechanism instead of relying on Java's default serialization. Correct implementation of Externalizable interface can improve performance of application drastically.
  4. How many methods Serializable has? If no method then what is the purpose of Serializable interface?
    1. Serializable interface exists in java.io package and forms core of java serialization mechanism. It doesn't have any method and also called Marker Interface in Java. When your class implements java.io.Serializable interface it becomes Serializable in Java and gives compiler an indication that use Java Serialization mechanism to serialize this object.
  5. What is Marker interfaces in Java and why required?
    1. Looking carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.
  6. While serializing you want some of the members not to serialize? How do you achieve it?
    1. if you don't want any field to be part of object's state then declare it either static or transient based on your need and it will not be included during Java serialization process.
  7. What will happen if one of the members in the class doesn't implement Serializable interface?
    1. One of the easy question about Serialization process in Java. If you try to serialize an object of a class which implements Serializable, but the object includes a reference to an non- Serializable class then a 'NotSerializableException’ will be thrown at runtime.

No comments:

Post a Comment