Marker Interface

Even it’s an old concept but still in use and we just ignore it in our regular development. Concept of marker interface it quite a bit old to discuss now, when the programming has boosted by the annotation based development. To understand more we have to take reference of interface of Java API which is Serializable. It is also called Empty Interface or Tagging Interface.

Marker interface is mainly aimed to provide metadata about the class during execution

So, Marker interface specifically used to provide information of class during runtime for which java does not have direct support. Considering Serializable interface which is a marker interface, it only provides the flag during the execution to the other Java library code that this class will going to appear exactly same after serialization and deserialization.

Marker interface does not have anything in it. No fields or methods and that’s why it is also called Empty interface

Marker interface provide tag or signal about the class, which allows java class library to provide special behavior to the implementing class. Actually, marker interface does not do anything with class during execution but the java class library code treat it differently if it has implemented marker interface. You can create your marker interface to treat implementing classes differently in your code but you can’t manage to have that special treatment from internal java code.Which is only the case for in built marker interfaces (of Java API) and not applicable for user defined marker interface.

Marker interface solely mark your class. Consider that your class Mango has implemented some marker interface Juicy.

interface Juicy {}

interface Fruits {}

class Mango implements Fruits, Juicy {}

class Banana implements Fruits {}

Now consider above example like this, you have created marker Juicy and your class Mango now tagged with Juicy and in some other place say at some Factory, which is checking type of your mango whether it is Juicy or not and if it is Juicy it treats it differently may be create juice from the mango. If the Fruit  is Banana and it’s not juicy do something else.

Marker interface should be replaced with the annotations (added in Java 1.5) and marker annotaion should be used instead of interface. As annotation is also capable of providing the metadata related to the class in a same manner. However one drawback I came across with the annotation is that we can have type of marker interface say for above example if we only want to pass the juicy fruits to the factory we can have following method,

public void checkJuicyFruits(Juicy juicy) {
   System.out.println("Checking Juicy Fruit...");
}

So this way we can have type defined by the marker interface and this won’t be available with annotations and due to this type we can manage to get most compile time errors that we won’t be able to catch until runtime in marker annotaion.

Marker Interfaces I know about in Java API are Serializable , Cloneable and Remote

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s