Java Marker Interface

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

Lets take the java.io.Serializable marker interface. It does not have 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 java.io.Serializable 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.

Is Runnable a marker interface?

I could not find any reference or definition to marker interface from Java specification or API. It looks like, ‘Marker Interface’ is a term coined by authors. This is one popular question asked in Java interviews.

“Runnable is not a marker interface”, you might say ‘run’ is used to start a method and this is a special instruction to the JVM. When you run a java class the java interpreter calls the ‘main’ method, because of this would you say every java class as “marker class”?

Marker Interface

“An interface is called a marker interface when it is provided as a handle by Java interpreter to mark a class so that it can provide special behaviour to it at runtime and they do not have any method declarations”.

I don’t think, in future there will be any new marker interfaces added.

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

Comments on "Java Marker Interface" Tutorial:

[…] for copy or to the least it should invoke the super.clone(). Also you have to implement Cloneable marker interface or else you will get CloneNotSupportedException. When you invoke the super.clone() then you are […]

Dear Joe
I have been reading books and blogs related to this topic of marker interface but I find everyone of them convoluted and unclear on the exact purpose. several questions and concern arise based on the explanations. For example consider marker interface serializable. How does the compiler differentiate between the two classes since their is no members defined in serializable interface.

Good explanation Joe !!
@Stefan : In case of a marker interface i guess there is a type check like :
if(myRef instanceOf Serializable) {
// do serialiazation stuff
…
}
with which the compiler knows about the marker interfaces (and their special meaning). As of java 1.5 marker interfaces are obsolete and annotations should be used otherwise.
Correct me if i am wrong.
Good luck :)

I think Java compiler won’t recognize the interface is a marker interface or not,only Java JVM will recognize it.If we are using serialization concept to save state of a object without implementing that class to the Serializable interface then we will get a runtime exception saying NotSerializableException.So from my point of view compiler won’t know anything about whether a interface is marker or not,only Java JVM is internally designed in such a way to provide some new ability to the class which implementing the marker interface.
Some examples of Java marker interfaces are :-
1) Serializable
2) Cloneable
3) RandomAccess
4) SingleThreadModel
5) EventListner

sir
you are told marker interfaces are not contain methods then externalizable class is containing the own methods but it is a extends of the serilazable . then im saying marker interfaces are may be contaning the methods or may not be containing methods

you can use the Externalizable Interface. It has two methods, writeExternal(..) and readExternal(..). You should override these two methods and provide your implementation (protocol) for serialization.

The main purpose to have marker interfaces is to create special types in those cases where the types themselves have no behavior particular to them.

This will give birth to another question like
If there is no behavior then why to have an interface?

Because the implementor of the class might only need to flag that it belongs to that particular type and everything else is handled/done by some other unit – either internal to Java (as in the case of Java supplied standard marker interfaces) or an app specific external unit.

as we know that marker interface doesn’t have any method. but the question is why to use marker interface like cloneable or Serializable type of interface. and how it works?? actually for an example using clone() method is not secure operation for jvm. so what marker interface does, it will give one mark or u can say one permission to jvm that plz grant this operation as a secure operation. and jvm permit this operation. so marker interface is used for marking the permission to any method.

There is absolutely nothing special about a marker interface — it’s just an interface with no methods. The only thing that makes them matter is that there’s code somewhere that checks “instanceof” against them; for example, the java.lang.Object version of clone() checks “this instanceof Cloneable” before it will actually do anything.

Consider 2 students X,Y in a class. X stood first in class, Even-though both are in same class, having similar properties and behavior. one can differ the both based on the stuff. There is no use of marking a person as smart. But it adds a special property to the person. Like marker Interface, JVM creates some interfaces that make a difference to classes. It just do nothing but adds a special behaviour.

if you extends any marker interface than your interface also become marker interface it will acquire all the properties of marker interface because some where in jvm it uses instanceof property to check. but we are still not able to change the behavior of jre to create directly our own marker interface without extending any existing marker interface

As me Concern Runnable is a marker Interface why because if our java class is not implements to the Runnable Interface then the object will act as normal object. that means it contains all state and behaviour of the class. otherwise if our class is implements Runnable Interface then the Implemented class object will get special behaviour given by the JVM. That means it will act as a Thresd unlike Process

As me concern an Interface called as a Marker Interface if and only if, the implemented class object will get special identity and behaviour at the time of runtime rather than normal behaviour.

if our class implements marker Interface , the implemented class object will get Special Identity and behaviour given by the JVM. For Ex.
A java class implemets Runnable Interface , then the Object will acts as a Thread
A Java class that implements Serializable, then the implemented class object will get such a behaviour it can tranfer across the Network
like this many more like
Java.lang. Clonable
Javax.servlet.SingleThreadModel

Ram, You are right in saying that compiler cant differentiate. Infact in the terms of marker interface, compiler has no role to play. Its only JVM who act based on the indication of marker interface NOT compiler. For example cloning is verified at runtime whether class implementing Cloneable or not. Same case with Serializable.

hi,
actually the clone method has the logic related to marker interface,
mean at the time of calling the clone() on the object it checks weather the object instantiated from cloneble interface or not ,if yes it creates the copy of that obj other wise it throws Exception.it checks like this.
if(o instanceOf clonableObgect )
{cloning}
else
{exception}
according to my study

Joe
I have occasionally visited this blog in past few years. I was teaching a new developer about marker interface and was doing preparation. When I read your blog I noticed below comments
From java 1.5 the need for marker interface is eliminated by the introduction of annotation feature. This is a common misunderstanding among lot of people. I happened to read Josh’s book Effective Java and in item 37 he clearly states the advantages of both and when to use each one of them. One difference I want to bring is
Marker interfaces define a type that is implemented by instances of the marked class; marker annotations do not. The existence of this type allows you to catch errors at compile time that you couldn’t catch until run-time if you used a marker annotation.
Hope I was able to add some value

Marker interface’s definition is given in all books.This term came from books only.And moreover if you wanna make your understanding better of marker interface.You may refer Effective java by Joshua Bloch or you may refer stack overflow.Because this tutorial is correct in a way but it’s incomplete

Runnable interface is not a marker interface because Runnable has a method run().Marker interfaces don’t have any methods.Marker interfaces are used to tell JVM to make class implementing marker interface to behave as per that interface.For Example,cloneable interface is a marker interface.Class implementing cloneable will tell JVM that this class can be cloned.

if no methods in interface then it is marker inter face but it is wrong we can use seralizable interface to use an interface
for example java.lang.EventListener
public void seralizable
{
void show()
{
system.out.println(“seralizable”);
}
}

i think marker interface is implemented by the jvm and jvm is responsible for the object to be serialize at the runtime .in compile time jvm has no idia about the class but when its compiled then every thing is cared by the JVM

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 of marker interfaces

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.

It’s very simple just define an interface without having any property or behaviour.
There are many advantages of Marker Interface in which one we can see easly that we can create the object or instantiate the Interface, which is simply not possible in Java.