2

Is that possible to write our own marker interface in java. I am writing a code like

public interface MyMarker {

}

Is that a marker interface?

If it is possible then how can I let JVM know that this interface is my own created marker interface?

Kushal
  • 8,100
  • 9
  • 63
  • 82
Abhisek
  • 715
  • 3
  • 13
  • 20
  • May I ask what kind of problem you want to solve with a marker interface? Maybe someone can suggest an alternative pattern. – Andreas Dolk May 17 '11 at 05:55

4 Answers4

13

Yes, that's a marker interface. You would test whether an object "implemented" it as simply as:

if (x instanceof MyMarker)

For a particular class (instead of object) you'd want

if (MyMarker.isAssignableFrom(otherClass))

You should consider using annotations instead of marker interfaces though. They're not always direct replacements, but in many cases they're used for the same goals, and annotations are (IMO) cleaner.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • but every marker interface has there own property.. how could i set my marker interface property? – Abhisek May 17 '11 at 05:27
  • @Abhisek: What do you mean? Could you give an example? – Jon Skeet May 17 '11 at 05:28
  • can you please give me a brief example of this? – Abhisek May 17 '11 at 05:29
  • like serializable interface it has a property of object percistency. like if i implement serializable interface in a perticuler class then we will be able to persist the value of the object of that class in permanent storage device. That kind of a unique property I want do with my marker interface .. is that possible? – Abhisek May 17 '11 at 05:33
  • 1
    @Abhisek: Well it entirely depends on what you're implementing. There's no *magic* behind this. You'd still need to write the code to implement whatever behaviour you want - you'd just use one of the tests shown above to determine whether an object or class used your marker interface. (As I say, an annotation would be preferable IMO.) – Jon Skeet May 17 '11 at 05:54
3

we can't say that an interface which doesn't have any method is a marker interface. because the word "Marker" itself signifies the meaning that " marking something". so i say, the interface(whatever may be it's contents) by implementing which if a class gains some extra or specialized behavior like allow object to store into a persistence storage(Serializable) OR allow an object to make it's duplicate or raplica(Cloneable) OR allow a user to implement only one method(like run()) instead of implementing nearly 4 t0 5 methods in a subclass in thread programming(Runnable) .

these are the specialized behavior which can be gained by an object when it implements those interfaces which are nothing but called as MARKER INTERFACE.

CONCLUSION

a marker interface may or may not contain methods...

it can also be called as tagged interface,dummy interface,empty interface....

0
  1. "Generally " Marker Interfaces are used to signal compile or JVM, of the additional metadata that might be needed while dealing with the object of "marker type".

  2. For example, if a class uses java.io.Serializable interface, the compile actually generates the implementation code for that marker interface for the class which is needed at run time by JVM while storing/marshaling that object.

  3. So I don't see any practical utility of custom marker interface (as it wont mean anything for compiler/JVM which works on a finite set of marker interfaces in java)

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • Can you please tell me what special is there on those type of iterface(serilizable etc) that whenever we use those in our class they act differently from other although those interface contains nothing (as they are so called marker interface) – Abhisek May 17 '11 at 08:08
  • @Abhisek: Sorry for late turned around. As I mentioned, marker interfaces are like **special instructions to compiler**, to generate additional code so that the special purpose of that marker interface is served by JVM at run time. For example, in case of java.io.Serializable, the compile puts some additional information in the class file which helps JVM in serialization. Thats why the marker interfaces act differently as they are treated differently by compiler. – Santosh May 17 '11 at 14:16
0

A class which implements an interface it gain some special quality that Interfaces are called Marker or Tagged Interfaces.

ex: A class implements Runnable interface that class acts as a Thread. so it is called Marker Interface.

 A class implements java.io.Serializable interface that clsass act to send object class which is needed at run time by JVM while storing/marshaling that object.

 A Class implements java.lang.Clonable interface that object ready to cloning. so java.lang.Clonabel interface is called Marker or Taged interface.

Some people belive All empty interface are Marker Interfaces. But it is NOT CORRECT.

Because we take java.lang.Runnable Interface it is not Empty interface it contain method called void run().

in java API maximum all Marker Interfafes are Empty like java.io.Serializable.

Some Marker interfaces or not empty like java.lang.Runnable.