In this section we will read about marker interface in Java.
This section will describe you the various aspects of marker interface viz. what is marker interface, use of marker interface, requirement of marker interface, annotation and marker interface.
What Is Marker Interface ?
Marker interface is an interface that marks the implemented class has to be added special behavior. Marker interface implemented class, like a tag, notifies the compiler for adding some special behavior in it at run-time. It is to be implemented in a class for associating the metadata with class. Marker interface has no any members. It is an empty interface. In Java some built-in Marker interfaces are as follows :
Use of Marker Interface
Marker interface can be used for following purposes :
Since JDK 1.5 introduction of annotation is a better choice to use than use of marker interface. for example use of @ThreadSafe annotation is better than the use of ThreadSafe marker interface.
Requirement of Marker interface
As we discussed above marker interfaces are used to inform something to Java compiler for adding special behavior to its implementing class. For example if a class implements the java.io.Serializable interface then the Java compiler is informed that the object of this class is to be serialized. This informs that the state of object of the class is to be persisted (saved). In an another example if in a class you want to use clone() method you would have to implement the java.lang.Cloneable interface to that class or its super class.
Annotation and Marker interface
An introduction of annotation from JDK 1.5 is become the first choice to use instead of marker interface. Java annotation is introduced with its various features and advantages over the marker interface. Annotation are used for adding metadata to the attributes or methods whereas, the marker interfaces can't be used for attributes and methods. Marker interfaces defines a type but, you can't do anything with the declared variable of that marker interface defined type whereas, annotation doesn't create a type and it is depend on the code operation that what has to be done with the annotation.
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: May 15, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles