Search code examples
javamarker-interfaces

What is the use of marker interface in java when we have no methods in it and how it gets into action?


I am confused about marker interfaces in java.

Could anyone please tell me when we have no methods in marker interfaces, then from where it is called. Do we have to implement this explicitly also.


Solution

  • It's just like any other interface.

    And used to type check the object at run time.

    For ex:

    And somewhere else Runtime realizes objects like

    if (SomeObjImpMarkerInterface instanceof SomeMarkerInterface ) {
            // Hey this object is that type
        } else {
            // Not that type.
        }