What is Marker Interface

What is Marker Interface:

  • A interface without any member is called Marker Interface.
  • It is just an empty interface.
  • The uses of Marker Interface is to send the special message to JVM about the class
Example of Marker Interface:

when we want to make a clone of any class object then we need to send the instruction to JVM that class is eligible for cloning. we can do this by implementing the marker interface.

Interface



package com.javaforecast4u.file;

public interface JFCUCloneable {

}

 Class implemented this interface



package com.javaforecast4u.file;

public class Employee implements JFCUCloneable{
      
       int empId;
       Employee(int empId){
              this.empId = empId;
       }
       public Employee empClone() throws CloneNotSupportedException {
              if(this instanceof JFCUCloneable){
                     return new Employee(empId);
              }
              else{
                     throw new CloneNotSupportedException("Clone Not support ");
              }
       }
}

Test Class
 

package com.javaforecast4u.file;

public class MarakerInterfaceTest {

       public static void main(String[] args) throws CloneNotSupportedException{
             
              Employee emp = new Employee(1001);
              System.out.println("EmpId:"+emp.empId);
              Employee emp1 = new Employee(emp.empId);
              System.out.println("EmpId:"+emp1.empId);
              System.out.println(emp == emp1);
             
       }

}


OUTPUT:
EmpId:1001
EmpId:1001
false