How to write our own marker interface in Java?

A marker interface in Java does not contain any methods. Java provides built-in marker interfaces such as Serializable, Cloneable, and EventListener, which the JVM recognizes.

Although we can create our own marker interfaces, they are unrelated to the JVM. Instead, we can incorporate checks using the instanceof operator to achieve similar functionality.

Create the empty interface :

interface customMarkerInterface
{    

}

Write a class and implements the interface :

class Test implements customMarkerInterface
{
   
    //do some task	
}

Write Main class to check the marker interface instanceof :

class Main {
    public static void main(String[] args) {
	        Test ob = new Test(){
	        if (ob instanceof Test) {
	            // do some task
	        }
	   }
}

Leave a Comment