Monday 16 June 2014

What is marker interface in Java? Why need marker interface?

           Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface. Example of marker interface is Serializable, Cloneable and Remote interface. Now if marker interface doesn't have any field or method or behavior then why would Java needs  it?



      Why Marker or Tag interface do in Java:--


1) Looking carefully on marker interface in Java e.g Serializable,Cloneable and Remote, it looks they are used to indicate something to compiler or JVM.
           So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Cloneable it performs some operation to support cloning. Same is true for RMI and Remote interface. So in short, Marker interface indicate signal or a command to Compiler or JVM.
          This is pretty standard answer of question about marker interface and once you give this answer most of the time interviewee definitely asked "Why this indication can not be done using flag inside a class?" this make sense right? Yes this can be done by using a boolean flag or a String but doesn't marking a class like Serializable or Cloneable makes it more readable and it also allows to take advantage of Polymorphism in Java.


   Where Should I use Marker interface in Java :--


           Apart from using built in marker interface for making a class Serializable or Cloneable. One can also develop his own marker interface. Marker interface is a good way to classify code. You can create marker interface to logically divide your code and if you have your own tool than you can perform some pre-processing operation on those classes. Particularly useful for developing API and framework like Spring or Struts.
         After introduction of Annotation on Java5, Annotation is better choice than marker interface and JUnit is a perfect example of using Annotation e.g @Test for specifying a Test class. Same can also be achieved by using Test marker interface.


      Another use Of Marker Interface in Java:


           One more use of marker interface in Java can be commenting. A marker interface called Thread Safe can be used to communicate other developers that classes implementing this marker interface thread-safe guarantee and any modification should not violate that.  Marker interface can also code coverage or code review tool to find bugs based on specified behavior of marker interfaces. Again Annotations  are better choice @ThreadSafe looks lot better than implementing ThreadSafe 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.

   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.

            In summary marker interface in Java is used to indicate something to compiler, JVM or any other tool but Annotation is better way of doing same thing.

5 comments:

  1. Ohayo,


    Muchas Gracias Mi Amigo! You make learning so effortless. Anyone can follow you and I would not mind following you to the moon coz I know you are like my north star.


    I want to make a project with a simulator of an enigma machine, the one used in the world war 2, but i actually don't know where to begin, because i don't know java very well.

    In particular, having an array like that char[] sRotor = {'G','N','U','A','H','O','V','B','I','P','W','C',' J','Q','X','D','K','R','Y','E','L','S','Z','F','M' ,'T'};, how can i say, in few codes, if a letter is x, turn it into y, if a letter is z, turn it into t... This for all the letters, creating like pairs.

    Then, talking about the rotors, when someone presses a letter, the first rotor should move, changing completely the code; when the first rotor changes from 26 (the last letter of the alphabet) to 1, the second rotor should move. Obviously, the same for the second and the third rotor. How can i write that?





    Very useful post !everyone should learn and use it during their
    Learning
    path.
    Thanks and Regards

    ReplyDelete
  2. Hola,


    What a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.

    I have two methods and they do almost same thing, but at some most inner nested level one of method have additional command.

    Java Code:
    method2(){
    ... same code ...
    for {
    ... same code ...
    if{
    ... same code ...
    }
    }
    }






    Follow my new blog if you interested in just tag along me


    in any
    social media platforms!


    Shukran,

    ReplyDelete
  3. Your wrong!!! I do not know where you are getting this information at? From java SE 7 Programmers Study Guide mentions that “interface is a public set of methods that must be implemented by the class that uses the interface.” An interface is an interface, if it has changed I should read this in this type of book mention above. If an interface like Cloneable has no public methods, means it does not have public methods, but any class that implements it must implement the Object.clone() as public. This is what it means!!! Please, don’t make something so simple into a complex discussion, especially at an interview!!

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Use of marker interface in java

    Marker Interfaces are used to indicate something to compiler/JVM. If JVM see that a class is a object of Marker Interface then it will perform some special operation. Take an example with Serializable, Clonnable marker interface, if JVM see a Class is Serialized/Clonnable then It will do some special operation on it, similar way if JVM sees one Class is implemented custom marker interface which is created by ourself then the JVM do some special operation. How it’d do the special operation,

    ReplyDelete