Monday, 11 May 2015

Marker interface in Java

A marker interface is an interface that has no method declarations or fields in it. It is used as a tag to let the compiler know it needs to add some special behavior to the class implementing marker interface. That is why marker interface is also known as tag interface.

Some of the examples of marker interface in Java are -

  • java.lang.Cloneable
  • java.io.Serializable
  • java.util.RandomAccess

Purpose of having marker interfaces in Java

The main purpose of marker interfaces is to create special types where types themselves have no behavior of their own.
In some case the implementing class might only need to flag that it belongs to that particular type and some special behavior should be added to it, how and what is handled by some other entity.
Like in case of Serializable marker interface, classes that need to be serialized just implement Serializable interface, now it is the task of ObjectOutputStream class to make sure that the classes that are implementing Serializable interface should be serialized properly.

Example using custom Marker interface

As we know marker interfaces define a type, that can be used with instanceof operator to test whether object is an instance of the specified type.

public interface MarkerEntity {

}

public boolean save(Object object) throws InvalidEntityFoundException {
   if(!(object instanceof MarkerEntity)) {
       throw new InvalidEntityFoundException("Invalid Entity Found, can't be saved);
   } 
   return db.save(object);
}

Here save method makes sure that only the objects of classes that implement the MarkerEntity interface are saved, for other types InvalidEntityFoundException is thrown. So here MarkerEntity marker interface is defining a type that adds special behavior to the classes implementing it

Creating our own marker interface

We can create our own marker interfaces. Please refer to above code for example.

Annotations or Marker interface

Though annotations can also be used now to mark classes for some special treatments but marker annotations are replacement for naming pattern not for Marker interfaces.
For Exp. In Junit before annotations it was required to begin the test methods name with "test" (thus requiring a specific naming pattern), now we can use @test marker annotation to specify junit test methods.

Though in some cases where marker interfaces were previously used to provide metadata about the class now marker annotations can be used.

But marker annotations can't fully replace the marker interfaces because; marker interfaces are used to define type (as already explained above) where as marker annotations do not.

Points to note -

  • Marker interface is an interface that has no method declarations or fields in it.
  • Marker interface is also known as tag interface.
  • Marker interfaces define a type.
  • We can create our custom marker interface.

That's all for this topic marker interface in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. interface in Java
  2. Difference between abstract class and interface
  3. interface default methods in Java 8
  4. interface static methods in Java 8
  5. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

3 comments:

  1. I hope you are Good,
    Thanks Anshudeep share deep knowledge of Marker Interface in Java
    http://www.javatportal.com/interface-in-java

    ReplyDelete
  2. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more .
    java training in chennai

    ReplyDelete