Sunday, April 8, 2012

Marker Interface

Marker interface is used as a tag to inform a message to the java compiler so that it can add special behavior to the class implementing it. Java marker interface has no members in it.

Lets take the marker interface. It doesn’t has any members defined it it. When a java class is to be serialized, you should intimate the java compiler in some way that there is a possibility of serializing this java class. In this scenario, marker interfaces are used. The java class which may be serialized has to implement the marker interface. In such way, we are intimating the java compiler.

From java 1.5, the need for marker interface is eliminated by the introduction of the java annotation feature. So, it is wise to use java annotations than the marker interface. It has more feature and advantages than the java marker interface.

We cannot create marker interfaces, as you cannot instruct JVM to add special behavior to all classes implementing (directly) that special interface.

Java Marker Interface Examples

  • java.lang.Cloneable
  • java.util.EventListener

1 comment:

  1. This write up is nice introduction to marker interface. For more details, I want to share my blog post on marker interfaces where I have discussed on various issues, advantages and interview questions for marker interfaces.