tags:

views:

238

answers:

5

Possible Duplicate:
What is the use of marker interfaces in Java?

What are marker interfaces and why are they used?

+12  A: 

There is an article on wikipedia on that subject which I suggest that you read.

klausbyskov
A: 

Marker interface provides extra functionalities.

they doesnt have body. they are empty inferfaces

SunilRai86
+5  A: 

It means an interface with nothing more than a name. No methods for you to implement. It's a way of putting your class in a certain category just by stating that it's in the category. I've seen it used for exception hierarchies, but I'm sure there are other applications.

Kate Gregory
I've used it myself to mark objects which I am guaranteeing will have an acyclic object graph. This came about when I was writing some serialization libraries and was too lazy to do proper cycle detection and unaliasing.
jasonmp85
+1  A: 

Example usage in java:

if (obj instanceof MarkerInterface) {
    // do marker interface related stuff
}
krock
This doesn't really explain the concept...
Peter Lillevold
+1  A: 

A so-called marker interface is a Java interface which doesn't actually define any fields. It is just used to "mark" Java classes which support a certain capability -- the class marks itself as implementing the interface. For example, the java.lang.Cloneable interface.

Marker Interface

In java language programming, interfaces with no methods are known as marker interfaces. Marker interfaces are Serializable, Clonable, SingleThreadModel, Event listener. Marker Interfaces are implemented by the classes or their super classes in order to add some functionality.

e.g. Suppose you want to persist (save) the state of an object then you have to implement the Serializable interface otherwise the compiler will throw an error. To make more clearly understand the concept of marker interface you should go through one more example.

Suppose the interface Clonable is neither implemented by a class named Myclass nor it's any super class, then a call to the method clone() on Myclass's object will give an error. This means, to add this functionality one should implement the Clonable interface. While the Clonable is an empty interface but it provides an important functionality.

SunilRai86