May 16, 2022

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 interfaces in Java are-

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

If you see the code for Cloneable interface in JDK, it is as given below.

public interface Cloneable {
}

As you can see there is no method or constant with in the interface, just empty body making Cloneable a marker interface in Java.

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.

else if (obj instanceof Serializable) {
	writeOrdinaryObject(obj, desc, unshared);
}

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 of object cloning 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

this instanceof RandomAccess ? new RandomAccessSubList<>(this, fromIndex, toIndex) : new SubList<>(this, fromIndex, toIndex)

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.

public interface MyMarker {

}

Which can then be used to define type.

public class MyMarkerImpl implements MyMarker {
  public void display() {
    System.out.println("display method of MyMarkerImpl");
  }
}

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.

public class Behavior {
  public static void main(String[] args) {
    MyMarker myObj = new MyMarkerImpl();
    MyClass obj = new MyClass();
    addSpecialBehavior(myObj);
    addSpecialBehavior(obj);
  }

  public static void addSpecialBehavior(Object obj) {
    if(obj instanceof MyMarker) {
      System.out.println("Adding special behavior to the object");
    } else {
      throw new IllegalArgumentException("Object not of type MyMarker");
    }
  }
}
Output
Adding special behavior to the object
Exception in thread "main" 
java.lang.IllegalArgumentException: Object not of type MyMarker
	at com.knpcode.programs.Behavior.addSpecialBehavior(Behavior.java:16)
	at com.knpcode.programs.Behavior.main(Behavior.java:9)

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

No comments:

Post a Comment