A marker interface is an empty interface in Java (it has no methods or fields). Its purpose is not to define behavior but to mark a class with metadata so that the JVM or frameworks can treat objects of that class differently.
Definition:
-
They existed before Java 5, when annotations did not exist.
-
A simple way to provide metadata about a class.
-
Used by JVM and libraries for special behavior.
Marker Interface | Package | Purpose |
Serializable | java.io | Marks a class as serializable (objects can be converted into byte stream). |
Cloneable | java.lang | Marks a class as cloneable using Object.clone(). |
Remote | java.rmi | Marks an object for Remote Method Invocation (RMI). |
EnterpriseBean | javax.ejb | Marks a class as an Enterprise JavaBean (legacy). |
SingleThreadModel (deprecated) | javax.servlet | Ensures servlet instance is accessed by only one thread at a time (legacy). |
(a) Serializable
(b) Cloneable
(c) Remote
Output:
Feature | Marker Interface | Marker Annotation |
Definition | Empty interface | Annotation without elements |
Introduced | Java 1.0 | Java 5 |
Examples | Serializable, Cloneable, Remote | @Override, @Deprecated, @FunctionalInterface |
Checked by | instanceof or Class.isAssignableFrom() | Reflection (isAnnotationPresent()) |
Flexibility | Only a tag, no metadata | Can carry metadata, attributes |
Usage Today | Legacy APIs | Preferred in modern frameworks (Spring, Hibernate, JPA) |
Output:
-
More flexible → Can add attributes & default values.
-
Metadata-rich → Useful for frameworks (Spring, Hibernate, JPA).
-
Cleaner syntax → No need for dummy interfaces.
-
Runtime Retention → Behavior can be customized at runtime using reflection.
-
Multiple annotations can be applied to the same class (a class can implement many interfaces too, but annotations are more expressive).
-
Marker Interface → Empty interface, used as a tag. Examples:
Serializable
,Cloneable
,Remote
. -
Marker Annotation → Modern replacement, introduced in Java 5. Examples:
@Override
,@Deprecated
. -
Custom Markers can be created both ways, but annotations are preferred today because they are more powerful, flexible, and metadata-driven.