Choose Your Language

Wednesday, 21 May 2014

How to create our own Marker Interface in java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package markerinterface;

/**
 *
 * @author Aravind Sankaran Nair
 */
public class MarkerInterface {
    public static void main(String args[]){
        MarkerImplementation markerImplementation=new MarkerImplementation();
       
      if(markerImplementation instanceof FirstMarker)  {
           System.out.println(markerImplementation.display());
      }
      if(markerImplementation instanceof SecondMarker)  {
          System.out.println(markerImplementation.display());
      }
    }
}
interface FirstMarker{
   
}
interface SecondMarker{
   
}
class MarkerImplementation implements FirstMarker{
   String display(){        
       return getClass().getInterfaces()[0].getSimpleName()+" interface is implimented";
   }
   
}

Output:
FirstMarker interface is implimented

1 comment: