What Is the Purpose of a Marker Interface

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.

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.

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

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

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.

can marker interface like serializable contain default methods?

A "Marker" interface is just a regular interface as far as Java is concerned. Thus, it can have default methods just as any (Java-8) interface can.

Now, as to whether this violates the principle of a Marker interface, I would have to say yes. A Marker interface should act as a flag of sorts, only identifying that a class meets some external criteria. Now, it can be a Marker interface and have abstract/default methods, but it will no longer purely meet the definition.

From Effective Java (Second Edition):

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



Related Topics



Leave a reply



Submit