What is a Marker Interface?
Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.
What Is the Use?
It is used to convey to the JVM that the class implementing an interface of this category will have some special behavior. It means it will tell jvm that this class have some functionality or capable of doing something.
Simple Understanding:
I will give you a non-technical example.Two things are there, a MOBILE and a CAR. I am having one room. What ever i will put into that room it will wash it give me back. if i will put my car then it will wash it and give me back,but if i put my mobile then it will be washed and it will be damaged. So what exactly I am going to do here is I will tell to the room that which is washable and which is not.So in this case we go for a MARKER interface.
In technical words i will create one marker interface “Washable” and i will implement this interface in Car but not in Mobile, so before washing the room should check is it a instance of washable interface or not.So by this we can overcome our problem.
More On Marker Interface:
In java we have the following major marker interfaces as under:
- Searilizable interface
- Cloneable interface
- Remote interface
- ThreadSafe interface
The marker interface can be described as a design pattern which is used by many languages to provide run-time type information about the objects. The marker interface provides a way to associate metadata with the class where the language support is not available.
A normal interface specifies functionality which an implementing class must implement. But a marker interface does not follow that pattern. On the other side, the implementing class defines the behavior. There are some hybrid interfaces which act as a marker interface along with some methods. But this type of design is confusing if not handled carefully.
Usage of Marker Interface in java:
Marker interface in Java e.g. Serializable, Clonnable and Remote is used to indicate something to compiler or JVM that the class implementing any of these would have some special behavior. Hence, if the JVM sees a Class is implementing the Serializable interface it does some special operation on it and writes the state of the object into object stream. This object stream is then available to be read by another JVM. Similarly if JVM finds that a class is implementing Clonnable interface, it performs some special operation in order to support cloning. The same theory goes for RMI and Remote interface. This indication (to the JVM) can also be done using a boolean flag or a String variable inside the class.
Apart from using the built-in marker interface, to mark a class as serializable or clonnable, we can also have our own marker interface. Marker interface is a good way to logically segregate the code and also if we have our own tool to perform some preprocessing operation on the classes. It is very useful for developing frameworks or APIs e.g. struts or spring.
With the introduction of annotation in java 5, annotation has become a better choice over maker interface.
The Thread Safe interface is a marker interface which can be used to communicate to other developers that classes implementing this marker interface gives thread-safe guarantee and any modification should not violate that. Marker interface also helps code coverage or code review tool to find bugs based on specified behavior of marker interfaces. Here also annotations are better choice. @ThreadSafe looks lot better than implementing Thread Safe marker interface.
In the next part I will come up with real time example of marker interface to make you understand better.Follow for more topics.

