What are marker Interfaces in Java ?
Points To Remember
  • A marker interface does not have anything inside it, its an empty interface.
  • A marker interface has a special meaning for JVM.
  • Since jdk v1.5 we can use annotations instead of Marker interfaces.
  • We can create custom Marker Interfaces but cannot .
Definition : Marker Interface
An interface that does not have any variable or method inside it and holds some special meaning for the JVM is called a marker interface. Whenever a class implements this interface, the JVM treats this class in a special way and adds some special behavior for this class. If a class implements a marker interface, then all its sub classes are also treated in a special way like the implemented class is treated.
There are three marker interfaces in Java
  1. java.io.Serializable - object implement it can be serialized using ObjectOutputStream.
  2. java.lang.Clonable - objects clone method may be called.
  3. java.util.RandomAccess- support fast (generally constant time) random access.
Marker Interfaces : What is so special about them
How does the JVM know that an interface is a normal interface or a marker interface ?  How does JVM treats it in a different way ? Well this is a very good question and may be asked in many interviews.

Lets understand this with a example,
Suppose that you are the JVM and the car shown in the image is a class that implements an interface Car. The JVM treats the following cases just like your mind treats them in the following cases.

interface Car{}

class CarA implements Car{
// Behavior of CarA
}

Case 1: Normal Interfaces.
Suppose the interface Car is a normal interface,So what can you say about this car ?

Nothing, this could be any car and can have any behavior, we cannot say anything about its price, mileage, comfort level or anything. So this is a normal class and our JVM treats this as a normal class with no special meaning or reference.

Case 2: Marker Interfaces.
Now, suppose that the interface Car was a marker interface, So what can you say about this car now( this car contains the logo of BMW)

Well, in this case you can say a few things about the car, like it would be a luxury car, expensive, good performance etc. Similarly the JVM understands that this is a special Car and treats it in a different way or in other words this Car now has a special meaning for the JVM.

Case 3: Custom Marker Interfaces.
Now, suppose we try to make Car as a marker interface of our own.
So now what can you say about this car.

Nothing, although we have given the car a tag just like in the case above(case 2, car with the bmw tag) but still we cannot predict its behavior or characteristics. Since we cannot add any special meaning to the Car, similarly we cannot instruct the JVM to treat any class in a different way.
Conclusion : How is Marker Interface different from a Normal Interface.
  • A Marker interface give a special meaning to the class.
  • We cannot create a Marker Interface since we cannot instruct the JVM to treat any class in a different way. However we can create a empty interface that we can use to tag certain classes that implements them.

No comments :

Post a Comment