4

Is there a list of standard marker interfaces in Java? I've read (in some Java book) that marker interfaces do not have any methods to implement , however when I did a google search - there are certain answers which specify that marker interfaces can indeed have methods. If that is the case then I think there is no difference between a regular interface and marker interface - would it be possible to clear my confusion :)

Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • 1
    I would say a marker interface doesn't add any methods. It can add Javadoc for methods in Object such as defining a behaviour for `String toString()` – Peter Lawrey Mar 25 '12 at 08:46
  • 1
    Hi , all your entries helped but unfortunately I can mark only one as correct. I have up voted others. Thanks! – Punter Vicky Mar 25 '12 at 15:56

7 Answers7

6

There is indeed no technical difference between "standard" and "marker" interfaces.

Normally you define an interface to define methods that implementing classes should have. If you don't specify any methods you call the interface a marker interface, since if only marks the class as having some property.

Examples of that are Serializable, Cloneable etc. Those interfaces don't define any methods themselves, but by convention and specification you have to option to implement some special methods related to them, e.g. some serializaton methods related to Serializable. The core Java libraries would then use reflection to check whether those methods exist if a marker interface is implemented.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Thanks Thomas , so technically marker interfaces would not be having any methods. Am I right? – Punter Vicky Mar 25 '12 at 08:37
  • @PunterVicky yes, _pure_ marker interfaces wouldn't have methods. As I said, it's only a name and marker interfaces are just plain old interfaces that mark a class as having some property and as AlexR said, since Java 5 you'd normally use annotations for that purpose. – Thomas Mar 25 '12 at 15:08
5

There is at least one: Serializable. I personally do not remember others. The technique of defining ta interfaces is old and almost obsolete since java 1.5 when annotations were introduced, so you can use annotation to "tag" class instead of empty interface.

AlexR
  • 114,158
  • 16
  • 130
  • 208
5

Marker interfaces are used as a tag to inform a message to the java compiler so that it can add special behaviour to the class implementing it and they do not have any method declarations.

The need for marker interface no longer exists since the introduction of the java annotation feature. Better use the more powerful java annotations than the marker interface.

Some examples of marker interfaces:

  • java.lang.Cloneable
  • java.io.Serializable
  • java.rmi.Remote
  • java.util.EventListener
Mistu4u
  • 5,132
  • 15
  • 53
  • 91
funcoder
  • 1,975
  • 20
  • 14
2

SigleThreadModel is also marker interface - ( I know it's deprecated now, but just for example I'm putting it's name here)

See more about it here

Dev
  • 21
  • 1
2

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

Java Marker Interface Examples

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

I don't know that there is a list of marker interfaces in the standard Java api. Whether marker interfaces can specify methods is explained well, I think, in the Wikipedia article "Marker interface pattern". Here's an excerpt that addresses your question directly:

Whereas a typical interface specifies functionality (in the form of method declarations) that an implementing class must support, a marker interface need not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. Hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used.

Aside from the Serializable interface mentioned in the article, there are few others. The only one I can remember off the top of my head is javax.security.auth.callback.

Just thought of another one: javax.security.auth.login.Configuration.Parameters. I'm pretty sure there are more.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Marker interfaces in Java SE 8:

Most widely used:

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

Remark: EventListener is officially known as 'tagging interface'.

Others:

java.util.concurrent.CompletableFuture.AsynchronousCompletionTask
java.sql.ParameterMetaData
javax.xml.stream.events.EndDocument
javax.management.loading.PrivateClassLoader
java.security.KeyStore.Entry
java.security.KeyStore.LoadStoreParameter   
java.security.KeyStore.ProtectionParameter
java.security.Policy.Parameters
javax.security.auth.callback.Callback
javax.security.auth.login.Configuration.Parameters
Igor Soudakevitch
  • 671
  • 10
  • 19