View Tanya  Sharma 's Profile
What is Marker interface? How is it used in Java?
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. 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. There can be some hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used. Java utilizes this pattern very well and the example interfaces are

java.io.Serializable - Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
java.rmi.Remote - The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. Only those methods specified in a "remote interface", an interface that extends java.rmi.Remote are available remotely.
java.lang.Cloneable - A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
javax.servlet.SingleThreadModel - Ensures that servlets handle only one request at a time. This interface has no methods.
java.util.EvenListener - A tagging interface that all event listener interfaces must extend.

The "instanceof" keyword in java can be used to test if an object is of a specified type. So this keyword in combination with Marker interface can be used to take different actions based on type of interface an object implements.
Asked by Tanya Sharma | Feb 15 2019 | Report this Report abuse
Replies (8)
View Diksha  Basu 's Profile
Now a days annotations are recommended in favor of marker interfaces but still marker interfaces have their own significance as explained in my blog post on marker interfaces
Feb 18 2019 | Report this Report abuse
View Nirupama  Pathak 's Profile
Now a days annotations are recommended in favor of marker interfaces but still marker interfaces have their own significance as explained in my blog post on marker interfaces
Feb 18 2019 | Report this Report abuse
View Khushi   Kumari 's Profile
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
The marker interface can be described as a design pattern which is used by many languages to provide run-time type information about the objects. The marker interface provides a way to associate metadata with the class where the language support is not available.

A normal interface specifies functionality which an implementing class must implement. But a marker interface does not follow that pattern. On the other side, the implementing class defines the behavior. There are some hybrid interfaces which act as a marker interface along with some methods. But this type of design is confusing if not handled carefully.
Feb 18 2019 | Report this Report abuse
View Chinmay  Hegde 's Profile
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
The marker interface can be described as a design pattern which is used by many languages to provide run-time type information about the objects. The marker interface provides a way to associate metadata with the class where the language support is not available.

A normal interface specifies functionality which an implementing class must implement. But a marker interface does not follow that pattern. On the other side, the implementing class defines the behavior. There are some hybrid interfaces which act as a marker interface along with some methods. But this type of design is confusing if not handled carefully.
Feb 17 2019 | Report this Report abuse
View Shivendu  Chatterjee 's Profile
bhai nahi aata book pad lo
Feb 17 2019 | Report this Report abuse
View Khushi   Kumari 's Profile
bhai nahi aata book pad lo
Feb 17 2019 | Report this Report abuse
View Roshan  shah 's Profile
You can recognize an object that implements an interface by using "instanceof". If you look at the source of java.io.ObjectOutputStream, you can see lines like "if (obj instanceof Serializable)" that do certain things fro serializable objects, and different things for other objects.
Feb 17 2019 | Report this Report abuse
View Havish  Goswami 's Profile
You can recognize an object that implements an interface by using "instanceof". If you look at the source of java.io.ObjectOutputStream, you can see lines like "if (obj instanceof Serializable)" that do certain things fro serializable objects, and different things for other objects.
Feb 17 2019 | Report this Report abuse