Marker Interface in java is an interface with no fields or methods within it. It is used to convey to the JVM that the class implementing an interface of this category will have some special behaviour. Hence, an empty interface in java is called a marker interface. Marker interface is also called tag interface by some java gurus.
Let's see a program what exactly a marker interface is :
interface P { } class MarkerDemo implements P { void show() { System.out.println("JavaRace"); } } class Mark { public static void main(String args[]) { MarkerDemo obj=new MarkerDemo(); if(obj instanceof P) obj.show(); else System.out.println("not permission"); } }
JavaRace
Here the object obj is created by MarkerDemo class which is also indirectly created by the interface P. Suppose I don’t want to print hello directly by calling show() method, I need some permission, that’s why we give some permission to the class MarkerDemo. If the object obj is the instance of P, then the method show() is called, otherwise else statement will be executed. So here if condition is true so the output is JavaRace.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc