Java › Interfaces & Abstract Classes
How does a marker interface (an interface with no methods at all, like Serializable or Cloneable) work, and what's the modern alternative approach (annotations) for achieving similar 'tagging' purposes?
AdvancedAnswer
A marker interface has no methods or fields at all, existing purely to 'tag' or mark implementing classes as having some property, checkable at runtime via instanceof (like `if (obj instanceof Serializable)`), used historically by APIs like Java's built-in serialization mechanism (checking Serializable) or Object.clone() (checking Cloneable) to determine whether a given operation is permitted for a given object -- the modern, generally preferred alternative for this kind of 'tagging' purpose is an annotation instead (like a custom @Cacheable annotation, checked via reflection rather than instanceof), since annotations can carry additional metadata/elements beyond a simple yes/no tag, can be applied at a finer granularity (to individual methods or fields, not just entire classes), and don't consume a class's limited 'interface budget' the way implementing an additional, purely-marker interface does (though marker interfaces DO have the notable advantage of being enforceable at compile time via generic bounds, something a runtime-only annotation check cannot achieve).
// Classic marker interface approach
public class User implements Serializable { } // tagged via 'implements', checked with instanceof
// Modern annotation-based tagging alternative
@Retention(RetentionPolicy.RUNTIME)
public @interface Cacheable { }
@Cacheable
public class Product { }
// Checked via reflection: someClass.isAnnotationPresent(Cacheable.class)Real-world example The JDK's own Serializable and Cloneable remain marker interfaces (a long-standing design predating annotations entirely), while a modern custom framework building similar 'tag this class as eligible for X behavior' functionality would typically choose a custom annotation instead, gaining the ability to attach additional configuration values and apply the tag at a finer method/field granularity that a class-level marker interface alone couldn't provide.