Continuing with other topics:

What is interface in Java & its purpose ?
Components of interface in Java

Here I am describing the another type of defining interface.

What is Marker Interface ?

Marker interface in Java is interfaces with no field or methods.
Marker interface are also called tag interface in Java.

What are Marker Interfaces available in Java ?

Serializable, Clonnable and Remote interface

What is the purpose of having Marker Interface ?

Marker interface indicate, signal or a command to Compiler or JVM that Class (Implemented from Marker Interface) is for special and dedicated purpose.
EX 1: JVM sees a Class is Serializable it performs some operation to support serialization.
EX 2: JVM sees one Class is implement Clonnable it performs some operation to support cloning.
EX 3: JVM sees one Class is implement Remote it performs some operation to support RMI  (Remote Method Invocation.

How to detect class implements Marker Interface ?

Using instanceof operator.
EX: (child instanceof Serializable) // Returns true only if it child variable class implements Serializable

What are advantages of having Marker Interface?

A marker interface called Thread Safe can be used to communicate with thread safe, those 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.

Can we define our own Marker Interface similarly to built in Marker Interfaces ?

Yes.

Apart from using built in marker interface for making a class Serializable or Clonnable. One can also develop own marker interface. 

Marker interface is a good way to classify code. Can be create marker interface to logically divide your code and if it is own tool than it can perform some pre-processing operation on those classes. 
Particularly useful for developing API and framework like Spring or Struts.

Thanks for reading post !!

 

Advertisements