BIG
DATA

JAVA

Java Serialization Interview Questions and Answers Part-1

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 
B

Basic Level Interview Questions

I

Intermediate Level Interview Questions

A

Advanced Level Interview Questions


B What is Serialization?

Java provides a mechanism, called object serialization where an object can be represented as a 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. The object can be restored at a later time, and even a later location. With persistence, we can move an object from one computer to another, and have it maintain its state.

Java Serialization

I What is the need of Serialization?

Serialization is usually used when the need arises to send your data over network or stored in files. By data I mean objects and not text. Now the problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not Java objects. Serialization is the translation of your Java object’s values/states to bytes to send it over network or save it.

B Do we need to implement any method of Serializable interface to make an object serializable?

No. Serializable is a Marker Interface. It does not have any methods.

B What are the advantages of Java serialization?

  • It is easy to use and relatively simple to understand
  • It facilitates the transportation of an object through a network
  • It helps to create a clone of an object
  • It is built into the system, hence do not need to rely on third-party tools, libraries, or configuration
  • The serialized stream can be encrypted, authenticated and compressed, supporting the needs of secure Java computing.
  • Serialized classes can support coherent versioning
  • There are many critical technologies that rely upon serialization

I What is Serializable interface in Java?

The Java's java.io.Serializable is a marker interface, means that it contains no methods or fields and serves only to identify the semantics of being serializable. Therefore, a class implementing Serializable does not have to implement any specific methods . It is just used to "mark" java classes that their state can be serialized or deserialized.

So your classes must implement this if they are to be serialized and deserialized. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.

public class MyClass implements Serializable {
    // not implementing any specific java.io.Serializable methods because
    // the interface has no methods to implement.
}

I What are the conditions to meet to serialize an object in Java?

For a class to be serialized successfully, two conditions must be met:

  • The class must implement the java.io.Serializable interface.
  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

A How to serialize an object in Java?

For a class to be serialized successfully, two conditions must be met:

  • The class must implement the java.io.Serializable interface.
  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

So if you want a class object to be serialized, all you need to do is implement the java.io.Serializable interface. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.

When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.

A Other than Serialization what are the different approach to make object Serializable?

Besides the Serializable interface, at least three alternate approaches can serialize Java objects:

  • For object serialization, instead of implementing the Serializable interface, a developer can implement the Externalizable interface, which extends Serializable. By implementing Externalizable, a developer is responsible for implementing the writeExternal() and readExternal() methods. As a result, a developer has sole control over reading and writing the serialized objects.
  • XML serialization is an often-used approach for data interchange. This approach lags runtime performance when compared with Java serialization, both in terms of the size of the object and the processing time.
  • Finally, consider a "roll-your-own" serialization approach. You can write an object's content directly via either the ObjectOutputStream or the DataOutputStream. While this approach is more involved in its initial implementation, it offers the greatest flexibility and extensibility. In addition, this approach provides a performance advantage over Java serialization.

A What happens if the object to be serialized includes the references to other serializable objects?

If the object to be serialized includes references to the other objects, then all those object’s state also will be saved as the part of the serialized state of the object in question. The whole object graph of the object to be serialized will be saved during serialization automatically provided all the objects included in the object’s graph are serializable.

A What happens if an object is serializable but it includes a reference to a non-serializable object?

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.

A Are the static variables saved as the part of serialization?

No. The static variables belong to the class are not the part of the state of the object so they are not saved as the part of serialized object.

B What is a transient variable?

Variables marked transient are not included in the process of serialization and are not the part of the object’s serialized state.

A What will be the value of transient variable after de-serialization?

It’s default value. e.g. if the transient variable in question is an int, it’s value after deserialization will be zero.

A If a class is serializable but its superclass in not, what will be the state of the instance variables inherited from super class after deserialization?

The values of the instance variables inherited from superclass will be reset to the values they were given during the original construction of the object as the non-serializable super-class constructor will run.

I To serialize an array or a collection all the members of it must be serializable. True /False?

True

A While serializing you want some of the members not to serialize, how do you achieve it?

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.

A What is the difference between Serializable and Externalizable interface in Java?

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.

A Suppose super class of a new class implement Serializable interface, how can you avoid new class to being serialized?

If Super Class of a Class already implements Serializable interface in Java then its already Serializable in Java, since you can not unimplemented an interface its not really possible to make it Non Serializable class but yes there is a way to avoid serialization of new class. To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method. This is another benefit of customizing java serialization process.

A What one should take care of while serializing the object?

One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.