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 doesnot has 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?

First point, there is no reference or definition about marker interface from Java specification/api. This is yet another inconclusive debate. ‘Marker Interface’ is a term coined by (book / web) authors. Since we don’t have Java’s definition it is left to us to arrive at a decent definition. This is one popular question asked in java interview. My answer to the interviewer, there are better questions ask than this to ask.

“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”?

Ads by Google

‘My’ definition for 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”.

In the above definition “and they do not have any method declarations” is added because, till now all the marker interfaces provided by java do not have method declarations. I dont 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.

Java Marker Interface Examples

  • java.lang.Cloneable
  • java.io.Serializable
  • java.util.EventListener

This Abstract and Interface tutorial was added on 29/05/2008.

«

»

Comments on “Java Marker Interface”

  1. Ram

    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.

    • Nitin

      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.

    • sree

      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

    • Anonymous

      I think , currently your in AKP. If your in akp , i want to meet u. please give u r contact no.

    • ramesh

      Hi guys,

      Anybody tell me,

      How to create MARKER INTERFACE in Java

      • Roushan Singh

        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.

    • ramesh

      Hi guys,

      Anybody tell me,

      How to create own MARKER INTERFACE in Java

    • ramesh

      Hi guys,

      Anybody tell me,

      How to create own MARKER INTERFACE in Java

      Thanks,
      Ramesh.

      • Umar Farook

        interfaces with no field or methods, in simple word we can say empty interface, is called Marker or Tag Interface. Marker interface is used as a tag to pass a message to java compiler so that it can add special behavior to the class that implementing it. Clonable and Serializable are example of Marker Interface. Now let’s come to our main topic, how can I create our own marker interface.

        Cheque.java

        1
        2
        public interface Cheque {
        }

        BankDraft.java

        1
        2
        public interface BankDraft {
        }

        Payment.java

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        public class Payment implements BankDraft{

        public void paymentByCheque() {
        System.out.println(“Payment By Cheque”);
        }

        public void paymentByBankDraft() {
        System.out.println(“Payment by Draft”);
        }
        }

        MainClass.java

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        public class MainClass {
        public static void main(String[] args) {
        Payment p = new Payment();

        if (p instanceof Cheque) {
        p.paymentByCheque();
        }

        if (p instanceof BankDraft) {
        p.paymentByBankDraft ();
        }
        }
        }

        Output –

        Payment by Draft

        Description –

        In above example, we have created two empty interfaces Cheque and BankDraft. And Payment class implemented BankDraft interface. In MainClass class both interface behave as tag, output of MainClass depends on what interface you have implemented in Payment class.

        In above case,

        1
        public class Payment implements BankDraft
        That’s why output is

        Payment by Draft

        If you have change as follows

        1
        public class Payment implements Cheque

        Then new output will be

        Payment by Cheque

  2. Prasad

    Good explanation Joe !!
    @Ram : 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 :)

  3. Alps

    Gud xplanation but have some queries.
    Wats the use of a Marker Interface.When shall we can use user defined interface as marker interface.

    • prasad

      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”);
      }
      }

  4. praneeth

    how the compiler differentiate normal classes and classes implementing these marker interfaces even though the marker interface dont have any members

  5. Shital

    Nice Explaination……
    one query- Can you Provide Example for getting the concept clearly….??

  6. Kavitha

    Hi frnds give me a clear clarification regarding marker interfaces..
    why use them,wat is the need..?

  7. Marker interface does n’t have any method declaration, It tells to the compiler “This class for that purpose”….
    ie. Cloneable, serializable, eventListener

  8. Anonymous

    public class myBean implements Serializable{
    private int id;
    private 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;
    }
    }

  9. 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;
    }

    }

  10. I think compiler won’t recognize the interface is a marker interface or not,only 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 JVM is internally designed in such a way to provide some new ability to the class which implementing the marker interface.
    Some examples of marker interfaces are :-
    1) Serializable
    2) Cloneable
    3) RandomAccess
    4) SingleThreadModel
    5) EventListner

    • om kumar

      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

      • Mitul

        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

  11. bharath

    please,i would like to know in a clarity about the “Marker Interface” with example.

  12. Anonymous

    I think the comment from :santosh (on October 5th, 2011 11:20 am), is correct.It should be providing a msg to JVM rather than compiler.

    • Anonymous

      Does it make any vital diffrence, whether it is sending messgae/information to the compiler or JVM? This topic is all about Marker Interface and Joe explained it very well :)

  13. Ankur

    Can you plz provide an example how we can use annotations in place of marker interfaces.

  14. RAJ

    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

    • anjaneyelu

      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

  15. muthu

    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 ?

    • Anjaneyelu

      As my knowledge Concern a marker Interface may contain methods for ex. Java.Lang.Runnable, Java.Lang. Externalizable and some more…..

  16. muthu

    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 DOC in Serializable/Cloneable as well as Runnable .

    So, Could you give your suggestion on this ?

  17. Brahmi

    Is Runnable is a marker interface or not?

  18. Brahmi

    Is Runnable is a marker interface or not?

    if yes why?

    • Anjaneyelu

      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

  19. Geetanjali

    I want Purpose of marker interface. I know What is marker Interface Can u tell me? ………

    • Anjaneyelu

      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

  20. kavitha

    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.

    • Prasanth Pillai

      Hi Kavitha,

      You may use deep cloning ( Deep Copy) which gives the same purpose of Serialization.

      Regards,
      Prasanth Pillai

  21. Joe

    @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.

  22. Anantha Prakash

    @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.

  23. pallav rajput

    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.

  24. kavitha

    Thank you so much Joe for explaination of my query,It is really nice site for Java people.Thanks lot for your effort.

  25. 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..

    If u want to create your own marker interface.check out my blog

    http://purejavasecrets.blogspot.com/2011/12/creating-your-own-marker-interface.html

  26. Joe

    @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.

  27. Daksh

    Good job done,

  28. joeeee, u are special, thanks….

  29. Sudeer Verma

    nice explanation of marker interface

  30. Krishna

    thanks for your stuff..

  31. Dhanesh

    Great captions for this topic. Crisp and fruitful stuff.

    Thanks.

  32. Anonymous

    Good one Joe

  33. krishna

    Hi , pls tell me

    what is the best practices of collection frame work?

  34. Anonymous

    how jvm identify the class implements MarkerInterface or not?i mean based on serialversion uid or instanceof operator.

  35. Jai

    Sir please explain anonymous inner class.

  36. neha

    please define java for me

  37. naveen

    but camparable interface is marker interface and has compareto() method how?

  38. arivoli

    Good explanation Joe !!

  39. Anonymous

    excellent material guidance

  40. Praveen Rohal

    excellent work Joe…

  41. Deepthi

    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.

  42. venkata

    Hi,

    How JVM understand serilization?
    I mean how this is implemented in JVM, As this is market interface no method will there and its just sign to JVM saying that to serilize object.

    Could you please tell me how serilization is done in JVM and how it understands by sign or keyword.

    thanks,
    Venkata

  43. lingesh

    site for getting on line ….
    upesh89.110mb.com/nDolphin/JAVA.HTM
    http://www.projecttopics.info/Java/Java_Projects.php

  44. satyanand singh

    excellent answer—-well defined—-way of documentation from begin 2 end was serial and it was damn xcelnt

  45. Vitthal Shirke

    Good explanation.

  46. shilly

    what is the use of marker interface?

  47. sri

    How to write our marker interface? what kind of changes we need to do sothat the compiler will recognize it as marker interface?

  48. How exactly we can find it is a marker interface(except behavior thing ).How can identify JVM. All interfaces are Marker interfaces, which are not have behaviors.

  49. Anonymous

    give some more examples

  50. kamalakkannan

    purpose of msrkerInterface?
    purpose of null Interface?
    pls any one help me……….

  51. atul

    Awesome sir!!

  52. Subbhu Reddy

    This article is confusing and not correct what is said above. Compiler never determines whether the interface is a marker Vs. not marker instead this process is evaluated by JVM at runtime.

    I think, the author of this article is totally confused b/n compiler and interpreter (runtime JVM)and misleading people.

  53. Fareed

    Good Explanation for market interface .

    Thanks Q Very Much

  54. Fareed

    Opps its marker interface not a market interface

  55. Paras Jain

    Your explenation language is vere good,

    Keep It Up…

  56. Paras Jain

    Can we Override constructer ?

    If Yes then how ?

    If no then why ?

    I am waiting for your answer…

  57. Anonymous

    How can we write our Custom marker inteface

  58. Mamta

    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.

  59. Siddesh Sahu

    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

  60. Mamta

    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)

  61. manoj

    Hi joe,
    We can create marker interface.
    @Local
    public interface ChartFacadeLocal extends IChartFacade{

    }

  62. Johnson George

    Nice Joie..

  63. abhishek

    thanks

  64. Ram

    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

  65. sita

    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

  66. DurgaGanesh

    I am totally get confused. here is my queries are…..

    Can we create User-defined maker interface. ?

  67. DurgaGanesh

    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.

    • sita

      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.

  68. Madhusudana

    Hi Joe Can U tell me the Process Of Creating My Own Marker Interface With An Example….

  69. naveenkumar

    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…

  70. Sri

    Mr Joe can you explain me Is Runnable a Marker interface if it is Why

  71. Sandeep shukla

    Dear Joe,

    I feel I owe to you. Thanks man for such nice and simple explanation I loved it….cheers !!

  72. Anonymous

    Thank u joe,i m praveen.
    plz tell me about the use of marker interface?

  73. Divya

    Very good article..
    Thanq joe

  74. muthukrishnan

    How compiler identify this is marker interface rather than normal interface?

  75. Manibharathi

    Hi Joe,
    Marker Interface is verfied by compile time or Runtime?

  76. 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.

    Read all about Marker interface in detail here ::

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

  77. peeyush gupta

    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

  78. 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

  79. Tarun

    Hi, explanation was OK. Apart from one wrong fact. Marker interface is not a term coined by books and writers. If you open source file of RandomAccess.java it is clearly written there that it is a marker interface.

  80. vasanth

    all are saying marker interface doesnot have any methods but it’s having two methods read() and write() . so how come it know , can u explain me?

Your Comment