All topics
library
intermediate

Marker Interfaces

Understand Serializable, Cloneable, and the marker interface pattern — and when annotations replaced it.

A marker interface has no methods. It exists so that other code can ask instanceof about it.

A marker interface = a wristband at a concert. It doesn't change what you can do, but security checks for it before letting you into the VIP area.

Key Concepts

1
    public interface Serializable { }        // genuinely empty
2
    // inside ObjectOutputStream
    if (!(obj instanceof Serializable))
      throw new NotSerializableException(obj.getClass().getName());
3
The three in the JDK worth knowing.
    Serializable    ObjectOutputStream refuses anything without it
    Cloneable       Object.clone() throws CloneNotSupportedException without it
    RandomAccess    marks a List with O(1) get, so algorithms pick
                    an indexed loop over an iterator
4
RandomAccess is the clearest example of the pattern paying off:
    if (list instanceof RandomAccess)
      for (int i = 0; i < list.size(); i++) use(list.get(i));
    else
      for (T t : list) use(t);
RandomAccess
5
On an ArrayList the indexed loop is faster. On a LinkedList it would be O(n^2), because every get walks from the head. The marker lets one algorithm serve both.
ArrayListLinkedListget
6
Marker interface or annotation?
    marker interface    is a type -- usable as a parameter or return type
                        checked by the compiler
    annotation          metadata only -- no type
                        checked at runtime by reflection
7
That is the deciding question. If you want this to compile:
8
    void archive(Serializable data) { ... }
9
you need an interface. An annotation cannot constrain a parameter, so the check would move to runtime and the caller would learn about it from an exception.
10
If it is only metadata, use an annotation. They can carry values, be repeated, and target fields or parameters, none of which an empty interface can do. @Entity and @Test would be far worse as markers.
@Entity@Test
11
Cloneable is the cautionary tale. It marks a class as cloneable without declaring clone(), which is protected on Object — so the interface promises a capability it does not expose. Effective Java recommends a copy constructor instead.
Cloneableclone()protectedObject
12
What the interviewer is probing.1. "What is a marker interface and when would you use one over an annotation?" Probing: whether you can state the deciding factor. Stalls: "They are interchangeable." Moves up: a marker defines a type, so it can constrain a parameter and be checked at compile time; an annotation is metadata checked at runtime.
13
2. "Why is Cloneable considered a design mistake?" Probing: whether you can criticise a JDK API. Stalls: "It works fine." Moves up: it marks a class as cloneable without declaring clone(), which is protected on Object — so it promises a capability it does not expose.
14
3. "What does RandomAccess achieve?" Probing: the clearest payoff of the pattern. Stalls: "Nothing much." Moves up: algorithms test for it and pick an indexed loop over an iterator, so one implementation serves ArrayList and LinkedList without being O(n^2) on the latter.
15
4. "How does ObjectOutputStream use Serializable?" Probing: the mechanism. Stalls: "It reads the interface." Moves up: an instanceof check before writing, throwing NotSerializableException otherwise — the interface itself declares nothing.