Recently, I was posed the question of “how can you declare a marker interface in Java.” This question lead me to a second question of “what is a marker interface” because the term was new to me, which was exactly the question that I ran to Google with. Surprisingly, the answer lead me to realize that I had actually come into contact with marker interfaces before, and so have you in all likelihood.
A marker interface could be more aptly called an empty interface because in reality it is just that– an interface with no methods or fields. Where I had experienced this kind of interface was with native interfaces included in Java packages; Cloneable is an example from the java.lang package and so is Serializable from the java.io package. These interfaces are empty and a class which implements these empty interfaces essentially tells the program that “I’m able to be cloned” or “My state can be saved to a file.” Marker interfaces are used in a way different from most interfaces in that they imply, or mark, that the class implementing that interface should be given some special treatment.
So…Can we create our own marker interface? Of course we can! Let’s make our own simple marker interface now.
First we create our empty interface, I named it MarkerInterface for simplicity.
public interface MarkerInterface {
}
Next we have a class which will implement MarkerInterface.
public class ClassImplementing implements MarkerInterface{
//Stuff could be written here
}
Lastly, we have our main class which has our main method in it. Before the main method, we have a method which will test if the class instance we call on uses MarkerInterface or not. Then in our main method, we create an instance of ClassImplementing, then we invoke the method isMarkerInterface on the instance of ClassImplementing.
public class MarkerInterfaceFunctionality {
static void isMarkerInterface(Object object) {
if (object instanceof MarkerInterface) {
System.out.println("This object is an instance of MarkerInterface!");
}
}
public static void main(String[] args) {
ClassImplementing classImplementing = new ClassImplementing();
isMarkerInterface(classImplementing);
}
}
Running the program will get us this response sent to the console, since ClassImplementing does implement MarkerInterface. If it didn’t, we wouldn’t get this message.

To answer the questions I posed in the introduction, a marker interface is a way in which you can separate your code and tell your program to treat classes which implement the interface differently from those that don’t. To declare a marker interface, you simply need to declare an interface which has no fields or methods inside of it–an empty interface.