Sandeep is a ITA at Tata Consultancy Services. He also is the author of java blog. Sandeep has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

Marker Interfaces in Java

03.16.2012
| 23130 views |
  • submit to reddit

Marker Interfaces in Java have special significance because of the fact that they have no methods declared in them which means that the classes implementing these interfaces don't have to override any of the methods. A few of the marker interfaces already exist in the JDK like Serializable and Cloneable.


One can also create their own custom interfaces which doesn't have any method. The purpose of these interfaces is to force some kind of functionality in the classes by providing some functionality to a class if it implements the marker interface.

A common question asked very frequently is about Runnable interface being marker or not. Runnable interface is not marker because Runnable interface has the public void run() method declared inside it. A very good example of marker interface is Serializable where the class implements can be used with ObjectOutputStream and ObjectInputStream classes.

The Java language specification doesn't itself define the term marker interface and the term has been coined by authors, developers and designers. One common question asked is if we can create a marker interface or not and the answer is yes because of following reason:

We can't create marker interface similar to Serializable or Cloneable but we can simulate the functionality by writing extra code around the custom marker interface.

Published at DZone with permission of its author, Sandeep Bhandari. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Santhosh Kumar T replied on Sat, 2012/03/17 - 12:06am

java.util.RandomAccess is also Marker interface from JDK other than you mentioned;

Nicolas Frankel replied on Sat, 2012/03/17 - 4:19am

Marker interfaces should be deprecated since Java 5: you should use annotations that play the exact role marker interfaces did before.

Carlos Hoces replied on Sat, 2012/03/17 - 5:45am

I do use marker interfaces under NetBeans Platform develoment for a very useful purpose.

A Service Provider needs an interface, but in cases where its implementation communicates to the "outside world" exclusively via a Publish/Subscribe decoupling pattern, the interface doesn't need to declare any methods at all.

I don't see any reason to claim they should be deprecated. 

Ronald Miura replied on Sun, 2012/03/18 - 5:53am in response to: Nicolas Frankel

You can use marker interfaces to restrict what can be passed as an argument. You can't do it wth annotations.

Manoj Kumar replied on Sun, 2013/09/22 - 12:26am

  Marker Interfaces are give about the implementing classes.

Interface in java defines a specific behavior while class can define both behavior and state. So Interface is pure behavior and class represent state and behavior. Interfaces are used in Java to specify the behavior of classes that implement those interfaces.. interfaces in Java that have no behavior  are known as marker interfaces. They have no method defined in them but are absolutely empty.






examples

Java.lang.Cloneable
If you want to added cloneable feature in a class ,that class needs to implement Cloneable interface.

java.io.Serializable
Serialization is nothing but s saving the state of an object to persistent storage as byte stream. 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.  The serialization interface has no methods or fields .

java.util.EventListener
A tagging interface that all event listener interfaces must extend.

Read all about Marker interface in detail here ::

http://efectivejava.blogspot.in/2013/09/marker-interface-cloneable.html

Muhammad Khojaye replied on Sat, 2013/12/21 - 3:36pm

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.