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 in Java.

Some of the examples of marker interface in Java are -

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

If you see Java code for Serializable or Cloneable interface, these are just empty interfaces.

Serializable interface

public interface Serializable {
}

Cloneable interface

public interface Cloneable {
}

So the question is what purpose does the marker interface serve when it is just an emmpty interface with no methods or variables of its own.

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 cases 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.

As example - If there is a class Address which you want to be serialized, Address class needs to implement Serializable interface.

public class Address implements Serializable{
   private String addressLine1;
   ....
   ....
}

Since Address class is implementing Serializable interface, it is of type Serializable. In the case of serializing an object, the entity which handles the serialization is ObjectOutputStream class. It is in the ObjectOutputStream class that it will check whether the object is of type Serializable.

if (obj instanceof Serializable)

If object is of type Serializable then object will be serialized otherwise java.io.NotSerializableException exception is thrown.

So, you can see Serializable which is a marker interface is just providing the type here, actual serialization is done by ObjectOutputStream class.

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. Object Cloning in Java
  5. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

5 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
  3. Hi Can you suggest some of good Selenium testing blogs like this.

    ReplyDelete
    Replies
    1. I am sorry but no idea about any Selenium testing blogs.

      Delete