Marker Interface in Java

Marker interface in Java is an interface with empty body. A marker interface does not contain constants or methods, examples of marker interface in Java are-

  • java.util.RandomAccess
  • java.rmi.Remote
  • java.io.Serializable
  • java.lang.Cloneable

Marker interface adds special behavior

Since marker interface is an empty interface so there are no methods to implement. It is used to indicate that the class implementing the marker interface belongs to that type but the marker interface itself doesn’t add any behavior (as it has no methods to implement).

In most of the cases the class that implements the marker interface just convey that some special behavior should be added to that class as it belongs to that type. For example if you read the description of some of the marker interfaces in Java-

Serializable– Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized.
Class that implements the Serializable marker interface in Java has the behavior of being serializable added to it. The class itself doesn’t do anything for that behavior, it’s the ObjectOutputStream class in Java where the object is checked whether it is of type Serializable or not using the instanceof operator. If it is of type Serializable then only the object is serialized.

Cloneable– A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

Here again you can see that the class just implements the Cloneable marker interface to add the behavior of cloning, actual behavior is provided by the native method Object.clone().

RandomAccess– Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm.

Here again you can see that the List implementations implement the marker interface RandomAccess to add behavior of fast random access. In the generic list class AbstractList you will find check for this type, as example

User defined marker interface (Custom marker interface)

You can also create your own custom marker interface in Java. It doesn’t need much, just create an interface with empty body.

Which can then be used to define type.

Then you can check for object of type MyMarker using instanceof operator while providing some special behavior to the object of that marker interface type. In the class, object of another class MyClass is also passed which doesn’t implement the marker interface MyMarker. In that case an exception is thrown.

Output

That’s all for the topic Marker Interface in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • Installing Java in Ubuntu
  • Difference Between Interface And Abstract Class in Java
  • SynchronousQueue in Java
  • How to Sort HashMap in Java
  • How to Read Delimited File in Java
  • Counters in Hadoop MapReduce
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.