Marker interfaces Java

After knowing what is serialization, let us know something more. To support serialization, package comes with interface Serializable and two classes ObjectOutputStream and ObjectInputStream.

1. class

Following is the class signature

public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants

The methods of ObjectOutputStream are capable to save the state of primitive data types to an OutputStream. Only the serialized objects are allowed to write.

Some write methods of ObjectOutputStream

  1. public void writeBoolean(boolean) throws;
  2. public void writeByte(int) throws;
  3. public void writeShort(int) throws;
  4. public void writeInt(int) throws;
  5. public void writeLong(long) throws;
  6. public void writeFloat(float) throws;
  7. public void writeDouble(double) throws;
  8. public void writeBytes(java.lang.String) throws;
  9. public void writeUTF(java.lang.String) throws;
  10. public final void writeObject(java.lang.Object) throws;

2. class

Following is the class signature

public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants

An ObjectInputStream is capable to deserialize primitive data and objects written earlier with ObjectOutputStream. Only serialized object (classes implementing or are allowed to read. Reading the object and restoring the state is known as deserialization (reverse of Serialization) and can done by the reading methods of ObjectInputStream.

An object written (to save the state) with writeObject() method of ObjectOutputStream can be read (otherway, restore the state) with readObject() method of ObjectInputStream.

Some reading methods of ObjectInputStream

  1. public boolean readBoolean() throws;
  2. public byte readByte() throws;
  3. public char readChar() throws;
  4. public short readShort() throws;
  5. public int readInt() throws;
  6. public long readLong() throws;
  7. public float readFloat() throws;
  8. public double readDouble() throws;
  9. public java.lang.String readLine() throws;
  10. public java.lang.String readUTF() throws;
  11. public final java.lang.Object readObject() throws, java.lang.ClassNotFoundException;

3. interface (introduced with JDK 1.1)

Following is the class signature

Compiled from “”
public interface {

Observe no methods in the interface.

The Serializable interface is very peculiar, in that, it does not contain any methods. The interface wihtout any methods is known as Marker interface or Tag interface or Empty interface. Some people may get a doubt of the usage of marker interface without implementing any methods in the subclass. A marker interface gives special instructions to the JVM to process the object suitable for a special task.

The subclasses of a serialized class are also serialized implicitly. An example can be taken from Java API.,

class Number implements Serializable
class Integer extends Number

Because the super class of Integer, the Number is serialized, the Integer objects are also serialized.

For example, implementation of Serializable interface gives special instructions to the JVM to process the object suitable to write to a file.

What we can do, in Java, with a serialized object?

1. An object can be written to a file.

Even if you try to write a non-serialized object to a file, the JVM does not allow to write and throws "NotSerializableException".

2. To send the object across a distributed network as in RMI (Remote Method Invocation).

In RMI, a distributed technology of Java, objects speak together (known as object-to-object communication). An object on a Java client (from one country) can communicate with another Java object (situated in a different country). For example, an ATM center from your locality can communicate with a Bank server present in a different country. In RMI, objects travel across network. These objects also should be serialized because data persistence and state of the object should be preserved.

Following list gives marker interfaces, Java comes with.

There are total 7 empty interfaces in Java API.

Maker interface name Functionality
1. the object is serialized
2. java.lang.Cloneable the object is cloned
3. javax.servlet.SingleThreadModel only one object of the servlet for one request is created
4. javax.ejb.EnterpriseBean the object can participate in transactions
5. java.util.RandomAccess the elements of the data structure can be randomly retrieved fast (introduced in jdk1.5)
6. java.rmi.Remote the object can invoke the methods on a remote machine
7. java.util.EventListener All event listeners extend this interface

Next program gives an idea of how to use Serializable, ObjectOutputStream and ObjectInputStream.

2 thoughts on “Marker interfaces Java”

Leave a Comment

Your email address will not be published.