A marker interface doesn't have an implementation.
However, a marker interface will typically influence the behaviour some other classes.
I found in googling that the marker interface is for sending some instructions to JVM?
That is incorrect.
What it is doing is providing information to either the JVM or to classes either provided by or running on the JVM. The information is NOT in the form of "instructions". Rather it is a label at the class level.
For example, when you declare a class as Serializable
, you are passing information to the ObjectInputStream
and ObjectOutputStream
that instances of that class can be serialized. You are passing that information in the form of the class itself; i.e. ObjectInputStream
and ObjectOutputStream
can use instanceof
(or equivalent) to test if an object is serializable.
Please help me with a real world example.
Serializable and Cloneable are the well-known examples in the Java class libraries.
There are other interfaces which have an "marker-interface-like" aspect. For example Closeable
and Autocloseable
both have a single close()
method that does the same thing. The difference is that AutoCloseable
is treated as a marker by a "try with resources" statement.
Finally, it is worth nothing that marker interfaces are frowned on by OO design purists. You can achieve the same end using Java annotations.