Tuesday, July 24, 2012

Java Marker Interface


I am surprised many times and wonder why Marker Interface is an interviewer’s favourite question. Probably this could be the most commonly asked question in an Java/J2EE interview. I guess I am not wrong to take this as my first main post for this blog J
Market interfaces are interfaces without any method declaration in them. An interface specifies the functionality/method the implementing class should support. But in the case of the Marker interface, the mere presence tells the JVM that some functionality needs to be performed.  Marker interfaces are also called ‘tag’ interface as they are used to tag a specific task /behaviour to the implementing class.

Examples of Marker interfaces are 

  • java.lang.Cloneable
  • java.io.Serializable
  • SingleThreadModel
  • java.util.Event listener

Let us understand this with examples.

  • java.lang.Cloneable
·                           Take the example of Cloneable interface. As with any Market interface Cloneable interface does not have any methods. An object inherits all the methods of Object class. Clone() method is defined in the Object class and is available for every object. The purpose of the Clone() is to duplicate the object.

Below the example
ExampleObj obj = new ExampleObj();
ExampleOb objclone = (ExampleObj)(obj.clone());

But when you run the above code, the compiler would throw an error. This is because you need to tell the JVM that object of ExampleObj class can be cloned. To do so, this class must implement the java.lang.Cloneable interface.

  • java.io.Serializable
·                          Similarly, if you would like to serialize a class, you need inform that to the JVM. By implementing  java.io.Serializable, the JVM tags the class and serializes.

  • SingleThreadModel
·                       Servlets are multithreaded. But if you would like the Servlet to run on a Single thread, then the servlet should implement the SingleThreadModel marker interface.

A Marker interface has no methods.  As an interface inherits all the methods of a super class/interface, the super class should also not have any methods or in other words the super class of a Marker interface should also be a Marker interface.

Runnable is not a Marker interface.
The next question asked immediately after you define a Marker interface is ‘Runnable a Marker interface?’ No, Runnable is not a Marker interface. The interface contains a method public void run().


Is Annotation better than Marker interface?
Yes, Marker interfaces are becoming obsolete and since JDK 1.5 Annotations are more used. Annotations provide a better way of adding/tagging information to a Class or method. To learn more, read my post on Annotation


Can you create your own Marker interface apart from the ones provided in Java? Marker interfaces are nothing but interfaces with no methods. But you cannot write an interface that would instruct the JVM on what to do unlike Serializable, Cloneable or SingleThreadModel. But you could write code to tag your object/data for specific purpose – see the below example.


Define these interfaces
public interface Cars{}
public interface Bikes{}
public interface Trucks{}


In the program where you would calculate the yearly sales of automobiles, add the below code. Now you are using the above interfaces only to class the automobiles or in other words for tagging.
for (Auto auto : allAuto) {
if (auto instanceof Cars) {
// perform calculations for number of new sold Cars
}
else if (auto instanceof Bikes) {
// perform calculations for number of new sold Bikes
}
else if (auto instanceof Trucks) {
// perform calculations for number of new sold Trucks
}
else {
// may be someother automobile.
}
}

No comments:

Post a Comment