Java Marker Interface

In this article we will discuss following about Marker Interface in Java.

– What is a Marker interface ?

– Why we need a marker interface or what is the use of marker interface ?

– How to create a custom marker interface and use it ?

Marker Interface
 

What is a Marker interface ?

 
When an interface has no members, its known as a Marker interface.

Its also known as a Tag interface.

 

Java API marker interfaces

 
Java API has many marker interface.

Here are some of them :
 

Serializable interface

It doesn’t have any methods, but instead tells the serialization mechanism that the class has the ability to be serialized.
 

Cloneable interface

If a class implements cloneable interface, then it allows cloning of the objects of that class.

 

Use of marker interfaces in java

A marker interface is used to mark the class with a special meaning that can be used in a particular context.

 
For example, when a class implements Cloneable marker interface, it informs the compiler that the class allows creation of clones of its objects. For actual cloning, a class has to override clone() method in Object.
 
Similarly, when a class implements Serializable, it informs that it can be serialized. The actual serialization is handled by ObjectInputStream and ObjectOutputStream classes though.
 
Reflection is used invoke the required methods for this.

 
Typically, a marker interface is used with the instanceof operator to check if a reference type variable refers to an object, whose class implements the marker interface.
 
For example, the writeObject() method in ObjectOutputStream does something like this :
 

if (obj instanceof Serializable) {
  writeOrdinaryObject(obj);

 

Create a custom marker interface in java

 

public interface MyMarker{

}

public class MyClass implements MyMarker {

  public void doFunnyStuff() {
    if (this instanceof MyMarker)
      System.out.println("This is a marker type");
    else
      ; //do nothing
  }

}

public class TestClass {

  public static void main(String[] args) {
    MyClass myRef = new MyClass();
    myRef.doFunnyStuff();
  }

}

 

Output :

This is a marker type

 

Summary

 
A marker interface does not have any members, but is used to provide special meaning to the JVM for particular context.

We can create a marker interface and provide implementation using instanceof as shown in example in this post.
 

You may also like following articles :

Top Java Tutorial : March 2016

Top 10 Java Puzzles of 2015

© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

You may also like...

2 Responses

  1. kavya says:

    It’s a kind of interface which has no method is known as marker interface. Serializable, Clonnable is the example of marker interface to create Marker Interface in Java
    Use of Marker Interface:
    Marker Interfaces are used to indicate something to compiler/JVM. If JVM see that a class is a object of Marker Interface then it will perform some special operation. Take an example with Serializable, Clonnable marker interface, if JVM see a Class is Serialized/Clonnable then It will do some special operation on it, similar way if JVM sees one Class is implemented custom marker interface which is created by ourself then the JVM do some special operation.

  2. smily says:

    one of the best explanation i can think of about marker interface in java..

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: