Master java skills

Marker Interfaces

Marker interfaces or Tagged interfaces

A marker or tagged interface is an empty interface. Which means it doesn’t have anything inside it. No constants, no abstract methods, no default and no static methods.

There are certain Java api language interfaces which are marker. For example

Clonable, Serializable, Remote

We can also define our custom marker interfaces. Let’s create a marker interface called Curable. Any desease that implements Curable interface will be marked as Curable type.

public interface Curable {
}
public class Dengue implements Curable {

//some code here

}
public class Corona {

	//some code here

}

What is the use of marker interfaces

Marker interfaces only defines a type. So, we can have a check on the objects of a class for that type, and then take some action. Let’s see below example

public class MarkerInterfaceTesting {

        public static void main(String [] args) {
                Dengue d1 = new Dengue();
		
		Corona c1 = new Corona();
		
		if(d1 instanceof Curable) {
			System.out.println("Dengue is a curable desease");
		}
		else {
			System.out.println("Dengue is not a curable desease");
		}
		if(c1 instanceof Curable) {
			System.out.println("Corona is a curable desease");
			
		}
		else {
			System.out.println("Corona is not a curable desease");
		}
        }

}
Output :

Dengue is a curable desease
Corona is not a curable desease