What Is the Use of Marker Interfaces in Java

What is the use of marker interfaces in Java?

In earlier versions of Java, Marker Interfaces were the only way to declare metadata about a class. For example, the Serializable Marker Interface lets the author of a class say that their class will behave correctly when serialized and deserialized.

In modern Java, marker interfaces have no place. They can be completely replaced by Annotations, which allow for a very flexible metadata capability. If you have information about a class, and that information never changes, then annotations are a very useful way to represent it.

Marker Interfaces in Java?

  1. Is the definition of a marker interface mentioned above in 1st point wrong? - It is correct in the parts that (1) a marker interface must be empty, and (2) implementing it is meant to imply some special treatment of the implementing class. The part that is incorrect is that it implies that JVM or the compiler would treat the objects of that class differently: you are correct in observing that it is the code of Java class library that treats these objects as cloneable, serializable, etc. It has nothing to do with the compiler or the JVM.
  2. instead of using the instanceOf operator why can't the method be something like writeObject(Serializable) so that there is a compile-time type checking - This lets you avoid polluting your code with the name of the marker interface when a "plain Object" is needed. For example, if you make a class that needs to be serializable, and has object members, you would be forced to either do casting or make your objects Serializable at compile time. This is inconvenient, because the interface is devoid of any functionality.
  3. How Annotations are better than Marker Interfaces? - They let you achieve the same purpose of conveying metadata about the class to its consumers without creating a separate type for it. Annotations are more powerful, too, letting programmers pass more sophisticated information to classes that "consume" it.

What exactly is marker interface in java?

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.

Ex.
1. java.io.Serializable is Marker interface.


  1. java.lang.Cloneable

why we need to use marker interface?

Shortly said, it is used to mark (or annotate) types with some information that the JVM compiler will use. For instance, the Serializable is a marker interfaces that a type must implement if it needs to have its state persisted (serialized and deserialized).

Marker Interfaces

clone() is defined in the java.lang.Object class which all classes extend from, however it is protected. This is actually a concrete method implementation that does a field by field clone of the object, but only if you've implemented the Cloneable interface to indicate this is allowed.

In practice many people override the clone() method so that they can make it public and allow cloning from outside the class.

This whole pattern is quite unusual and not something you would usually replicate, I can't think of many other examples in the JVM where there is a paired marker interface and method. From Java 5 onwards it's better to use annotations for markers. e.g. the @XmlRootElement used to mark a type as Jax-B serializable (post Java 5) vs the Serializable interface (pre Java 5) used to indicate a class is binary serializable.

Difference between Marker interface and empty interface?

Every Marker interface is an empty interface. Is vice-versa also correct?

Nope. An empty interface that has no meaning is not a marker interface ... in any useful sense. A marker interface has to denote something meaningful about the class that implements it.

And, in fact, according to some definitions a (hybrid) marker interface can contain methods (see the Wikipedia definition below). So a marker interface isn't necessarily an empty interface.


@Sudheep Vallipoyil says in his answer.

1) It is not possible to create a user-defined marker interface.

I disagree. That is based on a narrow definition of "marker interface" that is not the commonly accepted definition.

By contrast, here's how Wikipedia defines "marker interface" and the corresponding design pattern.

"The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata."

"To use this pattern, a class implements a marker interface (also called tagging interface), and methods that interact with instances of that class test for the existence of the interface. 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."

A second definition from Joshua Bloch says:

"A marker interface is an interface that contains no method declaration, but merely designates (or "marks") a class that implements the interface as having some property."

As you can see, in normal accepted usage, the term "marker interface" is not reserved for interfaces defined by the JVM. Anyone can define one.

See also:

  • Marker Interfaces in Java?

What is the purpose of marker interface?

Your understanding is correct. The marker interface also defines a type. It can thus be used in method signatures. For example, Hibernate's Session.get() method takes a Serializable as argument. It avoids passing a primary key that would not be serializable as argument.

Note that Cloneable is, retrospectively, seen as a bad design choice.

Serializable could certainly have been implemented with an annotation if those had existed when serialization was implemented.

Marker interfaces are, most of the time, an anti-pattern. An interface should define a polymorphic behaviour. A marker interface can be replaced by an annotation.



Related Topics



Leave a reply



Submit