09 / 13

What are Marker Interfaces (e.g., Serializable)? Are they an anti-pattern?

Difficulty: 3/10

Marker Interfaces

A marker interface is an interface with little or no behavioral methods that communicates metadata or capability about a type. Java's Serializable is the classic example. Marker interfaces are not automatically an anti-pattern. They can be appropriate when the type system itself needs to express a capability that framework or runtime behavior can inspect. However, if the information is purely metadata and does not benefit from type checking or polymorphic use, annotations or attributes may be a better choice.

javascript
  1. 1

    Marker interfaces communicate a type-level capability or metadata.

  2. 2

    Serializable is a well-known Java example.

  3. 3

    They provide compile-time type information that annotations may not provide in the same way.

  4. 4

    They can be useful when APIs need to accept only marked types.

  5. 5

    They become questionable when they are used only as arbitrary metadata without type-system value.

  6. 6

    Modern designs may prefer annotations/attributes for descriptive metadata.

Follow-up Questions

  • Why is Serializable considered a marker interface?
  • What is the difference between an annotation and a marker interface?
  • Can marker interfaces contain default methods?
  • When should metadata be represented through attributes?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.