skip to content

What is a marker interface in Java? Give examples and explain how it works.

level: juniorimportance: should knowfreq 62%

answer

  1. Empty interface = a tag, no methods
  2. Serializable -> NotSerializableException if absent
  3. Cloneable -> CloneNotSupportedException if absent
  4. Checked via instanceof or reflection by other code
  5. Annotations are the modern alternative (can carry params)

basics

~20 s

A marker interface is an empty interface with no methods. A class implements it just to 'tag' itself with a property, like being serializable. Code elsewhere checks 'instanceof' to see if the tag is present and behaves differently. Serializable and Cloneable are examples.

solid answer

~50 s

A marker interface (also called a tag interface) is an interface that declares no methods or fields. A class implements it purely to attach a type-level flag to itself. Other code then tests for that flag, usually with instanceof or via reflection, and changes behavior accordingly. The classic JDK examples are java.io.Serializable (the serialization machinery refuses to serialize an object whose class doesn't implement it, throwing NotSerializableException) and Cloneable (Object.clone() throws CloneNotSupportedException unless the class implements it). The marker is checked at runtime by the platform, not by the methods you'd normally call. Because it adds a real type, a marker interface gives compile-time typing in some APIs, but it cannot carry parameters. Since Java 5, annotations often replace markers for metadata, though markers remain when you want a true supertype the type system can reference.

go deeper

for a junior

Defines a marker interface as an empty interface used as a tag and names Serializable and Cloneable.

for a middle

Explains the runtime check (instanceof/reflection by external code) and the exceptions thrown when the marker is absent (NotSerializableException, CloneNotSupportedException).

for a senior

Compares marker interfaces to annotations, articulates the compile-time-type advantage, and cites RandomAccess as a behavioral-contract marker beyond pure metadata.

for a principal

Applies Effective Java guidance on marker-interface vs annotation, weighs inheritance/irreversibility and API-evolution implications, and designs tagging schemes for framework dispatch and binary compatibility.

## Definition An **interface** is normally a contract of methods. A **marker interface** (or **tag interface**) breaks that expectation: it is an interface that declares **no methods and no constants** — it is completely empty. A class "implements" it not to gain behavior but to **label itself** as having some property. The label is a fact about the *type*, checkable by other code. ```java public interface Serializable { } // empty: just a marker class User implements Serializable { /* now 'tagged' as serializable */ } ``` ## How the tag is actually used A marker by itself does nothing. Some other piece of code must **inspect** it and act. The two common ways: 1. **`instanceof` check** — `if (obj instanceof Serializable) { ... }`. 2. **Reflection** — querying the object's class for whether it implements the interface. The key insight: the **checker** lives somewhere else (often deep in the JDK or a framework), and it decides what the tag *means*. ## The two canonical JDK markers **`java.io.Serializable`** — Serialization is the process of turning an object into a byte stream (to save to disk or send over a network). The JDK's `ObjectOutputStream` will only serialize an object if its class implements `Serializable`; otherwise it throws `NotSerializableException` at runtime. The interface has no methods — implementing it is purely opting in to serialization. **`java.lang.Cloneable`** — `Object.clone()` produces a field-by-field copy of an object. But `clone()` checks at runtime whether the object's class implements `Cloneable`; if not, it throws `CloneNotSupportedException`. So `Cloneable` is the tag that says "clone() is allowed on me." (Note the historical oddity: the `clone()` method actually lives on `Object`, not on `Cloneable` — a common design criticism.) Other examples include `java.util.RandomAccess` (tags a `List` whose `get(i)` is fast/constant-time, so algorithms can choose index-based loops over iterators) and `java.rmi.Remote`. ## Why use a type instead of just a boolean field? A marker interface gives a **real type** the compiler and APIs can refer to. An API method can declare a parameter of the marker type to require, at compile time, that callers pass something tagged. A boolean flag could not do that. The marker is also inherited: every subclass of a marked class is automatically marked too. ## Limits and the annotation alternative A marker interface has limits: - It carries **no data** — it is just present or absent. You cannot attach parameters (e.g., a version number). - It is **inherited and irreversible** — once a class implements it, every subclass is tagged with no way to opt out. Since Java 5, **annotations** (e.g., `@Deprecated`, custom `@Entity`) are often used for metadata instead, because they can carry parameters and can target methods, fields, and parameters, not just types. So when should you still prefer a marker interface? When you want a genuine **supertype** that the type system can use — for instance, to constrain a method parameter at compile time, or when frameworks dispatch on `instanceof`. *Effective Java* recommends: use a marker interface when it defines a type that existing classes will implement and you want compile-time checking; use an annotation otherwise. ## Summary A marker interface is an empty interface used as a runtime/compile-time tag. The platform or a framework inspects the tag (via `instanceof` or reflection) and changes behavior. `Serializable` and `Cloneable` are the textbook examples; annotations are the modern alternative when you need richer metadata.

  • When would you prefer a marker interface over an annotation?
    When you need a real type the compiler can reference, for example to require a method parameter be 'tagged' at compile time, or when an API/framework dispatches on instanceof. Annotations win when you need to attach parameters or target methods/fields rather than types.
  • What happens if you serialize an object whose class does not implement Serializable?
    ObjectOutputStream throws java.io.NotSerializableException at runtime, naming the offending class.

saying these in an interview costs you the question

  • Thinking a marker interface contains methods (it is empty by definition)
  • Believing Serializable provides serialization logic (it only opts in; the JDK provides the machinery)
  • Saying clone() lives on Cloneable (it lives on Object; Cloneable just enables it)
  • Claiming annotations always replace markers (markers still win when you need a real supertype for compile-time checks)

context