Punter Vicky
Punter Vicky

Reputation: 17042

Marker Interfaces in Java

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 :)

Upvotes: 4

Views: 9236

Answers (7)

funcoder
funcoder

Reputation: 1975

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

Upvotes: 5

Igor Soudakevitch
Igor Soudakevitch

Reputation: 671

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

Upvotes: 0

Kangulla Kamala
Kangulla Kamala

Reputation: 21

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

Upvotes: 2

Dev
Dev

Reputation: 21

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

Upvotes: 2

Thomas
Thomas

Reputation: 88757

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.

Upvotes: 6

Ted Hopp
Ted Hopp

Reputation: 234867

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.

Upvotes: 1

AlexR
AlexR

Reputation: 115418

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.

Upvotes: 5

Related Questions