Java Serialization Interview Questions

Interview Questions on Serialization generally focus on the basics - How to serialize an object? Transient keyword and Serializable interface.

  1. What is Serialization?
  2. How do you serialize an object using Serializable interface?
  3. How do you de-serialize in Java?
  4. What do you do if only parts of the object have to be serialized?
  5. How do you serialize a hierarchy of objects?
  6. Are the constructors in an object invoked when it is de-serialized?
  7. Are the values of static variables stored when an object is serialized?

What is Serialization?

Serialization helps us to save and retrieve the state of an object.

  • Serialization => Convert object state to some internal object representation.
  • De-Serialization => The reverse. Convert internal representation to object.

Two important methods

  • ObjectOutputStream.writeObject() // serialize and write to file
  • ObjectInputStream.readObject() // read from file and deserialize

How do you serialize an object using Serializable interface?

To serialize an object it should implement Serializable interface. In the example below, Rectangle class implements Serializable interface. Note that Serializable interface does not declare any methods to be implemented.

Below example shows how an instance of an object can be serialized. We are creating a new Rectangle object and serializing it to a file Rectangle.ser.

class Rectangle implements Serializable {
    public Rectangle(int length, int breadth) {
        this.length = length;
        this.breadth = breadth;
        area = length * breadth;
    }

    int length;
    int breadth;
    int area;
}

FileOutputStream fileStream = new FileOutputStream("Rectangle.ser");
ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
objectStream.writeObject(new Rectangle(5, 6));
objectStream.close();

How do you de-serialize in Java?

Below example show how a object can be deserialized from a serialized file. A rectangle object is deserialized from the file Rectangle.ser

FileInputStream fileInputStream = new FileInputStream("Rectangle.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(
        fileInputStream);
Rectangle rectangle = (Rectangle) objectInputStream.readObject();
objectInputStream.close();
System.out.println(rectangle.length);// 5
System.out.println(rectangle.breadth);// 6
System.out.println(rectangle.area);// 30

What do you do if only parts of the object have to be serialized?

We mark all the properties of the object which should not be serialized as transient. Transient attributes in an object are not serialized. Area in the previous example is a calculated value. It is unnecessary to serialize and deserialize. We can calculate it when needed. In this situation, we can make the variable transient. Transient variables are not serialized. (transient int area;)

//Modified Rectangle class

class Rectangle implements Serializable {
    public Rectangle(int length, int breadth) {
        this.length = length;
        this.breadth = breadth;
        area = length * breadth;
    }

    int length;
    int breadth;
    transient int area;
}

If you run the program again, you would get following output

System.out.println(rectangle.length);// 5
System.out.println(rectangle.breadth);// 6
System.out.println(rectangle.area);// 0

Note that the value of rectangle.area is set to 0. Variable area is marked transient. So, it is not stored into the serialized file. And when de-serialization happens area value is set to default value i.e. 0.

How do you serialize a hierarchy of objects?

Objects of one class might contain objects of other classes. When serializing and de-serializing, we might need to serialize and de-serialize entire object chain. All classes that need to be serialized have to implement the Serializable interface. Otherwise, an exception is thrown. Look at the class below. An object of class House contains an object of class Wall.

House implements Serializable. However, Wall doesn't implement Serializable. When we try to serialize an instance of House class, we get the following exception.

class House implements Serializable {
    public House(int number) {
        super();
        this.number = number;
    }

    Wall wall;
    int number;
}

class Wall{
    int length;
    int breadth;
    int color;
}


Output:
Exception in thread "main" java.io.NotSerializableException: com.rithus.serialization.Wall
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)

This is because Wall is not serializable. Two solutions are possible.

  • Make Wall transient. Wall object will not be serialized. This causes the wall object state to be lost.
  • Make Wall implement Serializable. Wall object will also be serialized and the state of wall object along with the house will be stored.

Option 1

class House implements Serializable {
    public House(int number) {
        super();
        this.number = number;
    }

    transient Wall wall;
    int number;
}

Option 2

class Wall implements Serializable {
    int length;
    int breadth;
    int color;
}

With both these programs, earlier main method would run without throwing an exception. If you try de-serializing, In Example2, state of wall object is retained whereas in Example1, state of wall object is lost.

Are the constructors in an object invoked when it is de-serialized?

No. When a class is De-serialized, initialization (constructor’s, initializer’s) does not take place. The state of the object is retained as it is.

Are the values of static variables stored when an object is serialized?

Static Variables are not part of the object. They are not serialized.

If you loved these Questions, you will love our PDF Interview Guide with 400+ Questions.
Download it now!.

400+ Interview Questions in 4 Categories:
  1. Java : Core Java, Advanced Java, Generics, Exception Handling, Serialization, Threads, Synchronization, Java New Features
  2. Frameworks : Spring, Spring MVC, Struts, Hibernate
  3. Design : Design, Design Patterns, Code Review
  4. Architecture : Architecture, Performance & Load Testing, Web Services, REST Web Services,Security, Continuous Integration