Вы находитесь на странице: 1из 2

Java Object Serialization Interview Questions

How many methods in the Serializable interface? Which methods of Serializable interface
should I implement?

There is no method in the Serializable interface. It’s an empty interface which does not contain
any methods. The Serializable interface acts as a marker, telling the object serialization tools that
the class is serializable. So we do not implement any methods.

What is the difference between Serializalble and Externalizable interface? How can you
control over the serialization process i.e. how can you customize the seralization process?

When you use Serializable interface, your class is serialized automatically by default. But you can
override writeObject() and readObject() two methods to control more complex object serailization
process. When you use Externalizable interface, you have a complete control over your class's
serialization process. This interface contains two methods namely readExternal and
writeExternal. You should implement these methods and write the logic for customizing the
serialization process.

How to make a class or a bean serializable? How do I serialize an object to a file?


What interface must an object implement before it can be written to a stream as an object?

An object must implement the Serializable or Externalizable interface before it can be written to a
stream as an object. The class whose instances are to be serialized should implement an
interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected
to a fileoutputstream. This will save the object to a file.

What happens to the object references included in the object?

The serialization mechanism generates an object graph for serialization. Thus it determines
whether the included object references are serializable or not. This is a recursive process. Thus
when an object is serialized, all the included objects are also serialized alongwith the original

What is serialization?

The serialization is a kind of mechanism that makes a class or a bean persistent by having its
properties or fields and state information saved and restored to and from storage. That is, it is a
mechanism with which you can save the state of an object by converting it to a byte stream.

Common Usage of serialization.

Whenever an object is to be sent over the network or saved in a file, objects are serialized.

What happens to the static fields of a class during serialization?

There are three exceptions in which serialization doesn’t necessarily read and write to the stream.
These are
1. Serialization ignores static fields, because they are not part of any particular state.
2. Base class fields are only handled if the base class itself is serializable.
3. Transient fields.

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.

What is a transient variable?


Explain the usage of the keyword transient?


What are Transient and Volatile Modifiers

A transient variable is a variable that may not be serialized i.e. the value of the variable can’t be
written to the stream in a Serializable class. If you don't want some field to be serialized, you can
mark that field transient or static. In such a case when the class is retrieved from the
ObjectStream the value of the variable is null.

Volatile modifier applies to variables only and it tells the compiler that the variable modified by
volatile can be changed unexpectedly by other parts of the program.

What is Serialization and deserialization?

Serialization is the process of writing the state of an object to a byte stream. Deserialization is the
process of restoring these objects.

What is Externalizable?

Externalizable is an interface which contains two methods readExternal and writeExternal. These
methods give you a control over the serialization mechanism. Thus if your class implements this
interface, you can customize the serialization process by implementing these methods.