top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is markable interface in Java ?

+1 vote
1,096 views
What is markable interface in Java ?
posted Mar 6, 2016 by Deepak Jangid

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface. Example of marker interface is Serializable, Clonnable and Remote interface.
Marker interface indicate, signal or a command to Compiler or JVM(java virtual machine).

The marker interface is a design pattern, used with languages that provide run-time type information about objects. It provides a way 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, and code that interact with instances of that class test for the existence of the interface.

answer Mar 6, 2016 by Josita Sarwan
0 votes

Marker Interface in java is an interface with no fields or methods within it. It is used to convey to the JVM that the class implementing an interface of this category will have some special behavior.Hence, an empty interface in java is called a marker interface. Marker interface is also called tag interface by some java gurus. In java we have the following major marker interfaces as under:
Searilizable interface,Cloneable interface,Remote interface.ThreadSafe interface

answer Mar 9, 2016 by Ashish Kumar Khanna
0 votes

Looking carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.

marker interfaces java example tutorialThis is pretty standard answer of question about marker interface and once you give this answer most of the time interviewee definitely asked "Why this indication can not be done using a flag inside a class?” this make sense right? Yes this can be done by using a boolean flag or a String but doesn't marking a class like Serializable or Clonnable makes it more readable and it also allows to take advantage of Polymorphism in Java.

Where Should I use Marker interface in Java

Apart from using built in marker interface for making a class Serializable or Clonnable. One can also develop his own marker interface. Marker interface is a good way to classify code. You can create marker interface to logically divide your code and if you have your own tool than you can perform some pre-processing operation on those classes. Particularly useful for developing API and framework like Spring or Struts.

Another use of marker interface in Java

One more use of marker interface in Java can be commenting. a marker interface called Thread Safe can be used to communicate other developers that classes implementing this marker interface gives thread-safe guarantee and any modification should not violate that. Marker interface can also help code coverage or code review tool to find bugs based on specified behavior of marker interfaces.
Again Annotations are better choice @ThreadSafe looks lot better than implementing ThraedSafe marker interface.

In summary marker interface in Java is used to indicate something to compiler, JVM or any other tool but Annotation is better way of doing same thing.

answer Mar 10, 2016 by Karthick.c
...