Marker Interface doesn’t have any method or field or constant declared within it. So a marker interface is an empty interface.

Example :

  • Serializable
  • Cloneable, 
  • RandomAccess interface and 
  • Remote interface

Now one question that would immediately pop-up in your mind is,

Why would one declare an empty interface? 
What good is it of?

In general a common belief is that a marker interface is generally used to send some kind of signal to the JVM and instructs JVM to  treat the class implementing it in a special way.

Like, when a class implements Cloneable marker interface, it signals the compiler that the class allows creation of clones of its objects.

Generally, a marker interface is used with the instanceof operator to check if a reference type variable refers to an object whose class implements the marker interface.

You may also like to read about:

  • Why iterator is fail fast in java?
  • Can we call run() directly on thread without calling start?
  • Class variable vs instance variable
  • iterator vs list iterator
  • Why String is immutable in java?

Source link

What is marker Interface in java?

Leave a Reply

Your email address will not be published. Required fields are marked *