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.
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”?
“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 are closed for "Java Marker Interface".
[…] 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 :)
Gud xplanation but have some queries.
Wats the use of a Marker Interface in Java. When shall we can use user defined interface as marker interface.
how the Java compiler differentiate normal classes and Java classes implementing these marker interfaces even though the marker interface dont have any members
Nice Explanation……
one query- Can you Provide an Example for marker interface ….??
Hi frnds give me a clear clarification regarding Java marker interfaces..
why use them,wat is the need..?
Marker interface does n’t have any method declaration, It tells to the compiler “This class for that purpose”….
ie. Cloneable, serializable, eventListener
private class bean implements Serializable{
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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
please,i would like to know in a clarity about the “Marker Interface” with example.
I think the comment from :santosh (on October 5th, 2011 11:20 am), is correct.It should be providing a msg to Java VM rather than compiler.
Can you plz provide an example how we can use annotations in place of marker interfaces.
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
example :java.io.externalizable
Hi Joe,
I have been analyzing marker interface for more than one month .I found that they are two groups of people wrote solution
Group I : Marker interface does not contain any methods.It is just for giving information to compiler that this class will do seralization/cloneable operation.
Group II : Runnable is a marker interface.It has method of run().So,marker interface need not be empty methods.
I found that there is no keyword used as like ‘marker’ in java api in Serializable/Cloneable as well as Runnable .
So, Could you give your suggestion on this ?
Is Runnable is a marker interface or not?
I want Purpose of Java marker interface. I know What is marker Interface Can u tell me? ………
Is there any other way to make Serializable other than implementing Serializable Interface.I had interview today morning they asked this question.Can any body pls help me.
@Kavitha,
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.
@Geetanjali,
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.
Thank you so much Joe for explaination of my query,It is really nice site for Java people.Thanks lot for your effort.
As far as I have researched,Marker Interface means an interface which can provide special behavior to its child object.
The presence of Method is not really necessary,But the behavior is Important.
Some people say interface w.o method is called marker interface.
My question back to them is,if u define an interface w.o any method,will JVM treat it as marker.Certainly not..
@Suraj,
“Runnable is not a marker inteface”.
I have added my comment in detail at the end of the article.
Thanks for stopping by and adding the comment.
Good job done, the best Java tutorial for marker interface in net.
joeeee, u are special, thanks….
nice explanation of Java marker interface
thanks for your stuff..
Great captions for this topic. Crisp and fruitful stuff.
Thanks.
Good one Joe
Hi , pls tell me
what is the best practices of Java collection frame work? can you write a Java tutorial on this?
how Java jvm identify the class implements MarkerInterface or not?i mean based on serialversion uid or instanceof operator.
Sir please explain anonymous inner class.
please define java for me
but camparable interface is marker interface and has compareto() method how?
Good explanation Joe !!
excellent material guidance
excellent work Joe… the best Java tutorial … thanks.
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.
Hi,
How Java JVM understand serialization?
I mean how this is implemented in Java JVM, As this is market interface no method will there and its just sign to JVM saying that to serialize object.
Could you please tell me how serialization is done in Java JVM and how it understands by sign or keyword.
thanks,
Venkata
excellent answer—-well defined—-way of documentation from begin 2 end was serial and it was damn xcelnt
Good explanation.
what is the use of Java marker interface?
How to write our marker interface? what kind of changes we need to do sothat the compiler will recognize it as marker interface?
How exactly we can find it is a Java marker interface(except behavior thing ). How can identify Java JVM. All interfaces are Marker interfaces, which are not have behaviors.
Can you give some more real time examples in Java for marker interface
purpose of Java markerInterface?
purpose of null Interface?
Awesome sir!! I a beginner in Java and I am learning using your Java tutorials. Thanks a lot sir.
this marker interface process is evaluated by Java JVM at runtime. thanks for the tutorial.
Good Explanation for market interface .
Thanks Q Very Much
Opps its marker interface not a market interface
Your explanation and language is very good, easy to understand
the best Java tutorials collection..
Keep It Up…
Can we Override a constructor in Java?
If Yes then how ?
If no then why ?
I am waiting for your answer…
How can we write our Custom Java marker inteface
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
So, If a extend a marker interface then my interface is also a marker interface.
Can I declare methods in that?
(Marker interface should not have a single method)
yes !! I will agree with this statememt.ie A Marker interface may or may not contain methods for ex. The externalizable interface contain readInternal() and writeExternal() methods
As my knowledge Concern a marker Interface may contain methods for ex. Java.Lang.Runnable, Java.Lang. Externalizable and some more…..
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
Hi joe,
We can create marker interface.
@Local
public interface ChartFacadeLocal extends IChartFacade{
}
Nice Joie..
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.
Its not correct. compiler does not check or complain related to marker interface mandate.
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
thanks
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
I am totally get confused. here is my queries are…..
Can we create User-defined maker interface. ?
I am totally get confused. here is my queries are…..
Can we create User-defined maker interface. ?
Does maker interface contains any methods or not?
Is this true to state as “Runnable interface is marker interface” ?
Thanks.
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.
Hi Joe Can U tell me the Process Of Creating My Own Marker Interface With An Example….
please help me..
I want my own interface as marker interface. i will implements that interface to my class, than my class as active like seriazable object…
Mr Joe can you explain me Is Runnable a Marker interface if it is Why
Dear Joe,
I feel I owe to you. Thanks man for such nice and simple explanation I loved it….cheers !!
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”);
}
}
All your Java tutorials are excellent. Are you providing any video tutorials?
what is markerinter face………
Hi Kavitha,
You may use deep cloning ( Deep Copy) which gives the same purpose of Serialization.
Regards,
Prasanth Pillai
[…] you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag […]
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
Thank u joe,.
The best tutorial for marker interface?
Does it make any vital difference, whether it is sending message/information to the compiler or Java JVM? This topic is all about Marker Interface and Joe explained it very well :)
Very good article..
Thanq joe
How compiler identify this is marker interface rather than normal interface?
Hi Joe,
Marker Interface is verfied by compile time or Runtime?
Marker interface has some special privilleges in JVM.To enforce some licence mechnisam to
secured process.Not possible to create user defined marker interface. They areregisterd in
JVM
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 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.
Thanks for this nice tutorial. Can I create my own marker interface?
Hi guys,
Anybody tell me,
How to create own MARKER INTERFACE in Java
Thanks,
Ramesh.
Dear joe ,
suppose i create marker Interface name “MyInterface”
in this case what is the role of JVM for treating the class that implements MyInterface
hello sir i have one question how to make a custom marker interface . and how to show to jvm its marker interfCE AND ADD some special behavior to the class implementing
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.
Hi, explanation is good. Marker interface is not a term coined by books and writers. In the Java API source file of RandomAccess.java it is written that it is a marker interface.
This the best tutorial for marker interface. Thanks Joe for all your Java tutorials. Good work.