I know marker interface in java. It is used to define a specific behaviour about a class. For example, Serializable interface has the specific ability to store an object into by
Marker interface has no method. Java has built-in marker interface like Serializable
, Clonable
& EventListner
etc that are understand by JVM.
We can create our own marker interface, but it has nothing to do with JVM, we can add some checks with instanceOf
.
Create the empty interface
interface Marker{ }
Write a class and implements the interface
class A implements Marker {
//do some task
}
Main
class to check the marker interface instanceof
class Main {
public static void main(String[] args) {
A ob = new A(){
if (ob instanceof A) {
// do some task
}
}
}