An interface that does not contain methods, fields, and constants is known as marker interface. In other words, an empty interface is known as marker interface or tag interface. It delivers the run-time type information about an object. It is the reason that the JVM and compiler have additional information about an object. The Serializable and Cloneable interfaces are the example of marker interface. In short, it indicates a signal or command to the JVM.
The declaration of marker interface is the same as interface in Java but the interface must be empty. For example:
- public interface Serializable
- {
-
- }
There are the two alternatives of marker interface that produces the same result as the marker interface.
- Internal Flags: It can be used in place of marker interface to indicate any specific operation.
- Annotations: Since Java 5, marker interfaces are omitted. Instead of marker interface, Java 5 provides the annotations to achieve the same results. It allows flexible metadata capability. Therefore, by applying annotations to any class, we can perform specific action.