Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is a marker interface? Give JDK examples and explain how Serializable and Cloneable work.

A marker interface has no methods; implementing it labels a class as having some property that other code checks. ObjectOutputStream refuses to serialize an object whose class does not implement Serializable, throwing NotSerializableException, and Object.clone throws CloneNotSupportedException unless the class implements Cloneable. RandomAccess tells algorithms that indexed access is fast so they can choose an indexed loop instead of an iterator. Because a marker defines a type, it can be checked at compile time, which annotations cannot do.

Why this question is asked

Marker interfaces are one of the oldest patterns in Java, and they still affect everyday code: every JPA entity or DTO that implements Serializable, every class that tries to support clone(), every algorithm in Collections that runs faster on an ArrayList. Interviewers ask about them to see whether we understand that an empty interface can still change behaviour, how the JDK's best-known markers work, and when an annotation would be the better tool. In this answer, we will define marker interfaces, look inside Serializable, Cloneable and RandomAccess, and compare markers with annotations.

1A Quick Recap: What a Marker Interface Is

A marker interface is an interface with no methods at all. A class implements it not to gain any behaviour, but to make a statement about itself: I can be serialized, I can be cloned, I support fast indexed access. Some other piece of code, a library or the JVM, then checks for that statement with instanceof or an equivalent, and behaves differently depending on the answer.

Java
public interface Serializable { } // the whole interface: no methods
public interface Cloneable { }
public interface RandomAccess { }
class Customer implements Serializable { String name; }
System.out.println(new Customer() instanceof Serializable); // true

So the behaviour lives elsewhere. The marker is just a label that can be checked.

2Serializable: ObjectOutputStream Checks the Label

Java's built-in serialization turns an object graph into bytes with ObjectOutputStream. Before writing an object, it checks whether the object's class implements Serializable. If not, it throws NotSerializableException, naming the class. The reason for requiring an explicit opt-in is that serialization exposes private fields and creates objects without running constructors, so a class should agree to it on purpose.

Java
class Address { String city = "Pune"; } // not Serializable
class Customer implements Serializable {
String name = "Asha";
Address address = new Address();
}
try (var out = new ObjectOutputStream(new ByteArrayOutputStream())) {
out.writeObject(new Customer());
}
// java.io.NotSerializableException: Address
Step throughHow writeObject treats each object it reaches
  1. Is the object null, a String, an array or an enum?
    YesWrite it with special handlingNoCheck the class
  2. Does its class implement Serializable?
    YesWrite its non-transient fields, recursing into eachNoThrow NotSerializableException
  3. Next object in the graphRepeat for every reachable object

3 steps in order. Play them, or step through them one at a time.

Notice that the check applies to every object in the graph, not only the top one. Customer is marked, but its Address field is not, so the write fails on Address. Marking a class Serializable also commits us to its serialized form, which is why many teams now prefer explicit formats such as JSON for data that crosses process boundaries.

3Cloneable: Object.clone Checks the Label

Object has a protected clone() method that makes a field-by-field copy of an object. It only works if the class implements Cloneable. Otherwise it throws CloneNotSupportedException. The marker changes the behaviour of a method in a different class, Object, which is an unusual and much-criticised design, but it is how cloning works.

Java
class Point implements Cloneable {
int x = 1, y = 2;
@Override public Point clone() throws CloneNotSupportedException {
return (Point) super.clone(); // works because Point implements Cloneable
}
}
class Size {
int w = 3;
Object copy() throws CloneNotSupportedException { return super.clone(); }
}
System.out.println(new Point().clone().x); // 1
new Size().copy(); // CloneNotSupportedException: Size

Also notice that Cloneable itself does not declare clone(). Implementing the marker does not make clone() public or callable from outside; the class still has to override it. That is part of why cloning is considered awkward, and copy constructors or static copy factories are usually preferred today.

4RandomAccess: A Hint for Algorithms

RandomAccess marks list implementations whose get(int) is fast, constant time, such as ArrayList. LinkedList does not implement it, because getting element 50,000 means walking 50,000 nodes. Generic algorithms in the JDK check the marker to pick a strategy: an indexed loop for random access lists, an iterator for the others.

Java
static <T> int count(List<T> list, Predicate<T> p) {
int n = 0;
if (list instanceof RandomAccess) {
for (int i = 0; i < list.size(); i++) if (p.test(list.get(i))) n++; // fast for ArrayList
} else {
for (T t : list) if (p.test(t)) n++; // avoids O(n^2) for LinkedList
}
return n;
}
System.out.println(new ArrayList<>(List.of(1, 2, 3)) instanceof RandomAccess); // true
System.out.println(new LinkedList<>(List.of(1, 2, 3)) instanceof RandomAccess); // false

Collections.binarySearch, shuffle and reverse use this check internally. The marker never forces any behaviour; it gives algorithms the information to choose well.

5Marker Interfaces Versus Annotations

Since Java 5, annotations offer another way to label code, and many newer frameworks use them: @Entity, @Component, @FunctionalInterface. So when is a marker interface better? The key difference is that a marker interface defines a type. We can write a method parameter of that type and the compiler will reject anything that is not marked.

Java
void store(Serializable value) { /* ... */ } // compile-time check: only marked objects allowed
store(new Customer()); // compiles
store(new Address()); // compile error: Address is not Serializable
@Retention(RUNTIME) @Target(TYPE) @interface Auditable { String category() default "general"; }
// an annotation can carry data and be placed on methods or fields, but it is checked only by reflection

Annotations win in other ways. They can carry attributes, like a category; they can be placed on methods, fields and parameters, not just classes; and they can be added without changing the type hierarchy. So the rule of thumb is to use a marker interface when we want the compiler to enforce that only marked objects are passed somewhere, and an annotation when the label is metadata read by a framework at run time. Interestingly, ObjectOutputStream.writeObject takes Object rather than Serializable, so the JDK does not even use the compile-time benefit of its most famous marker.

6Follow-up questions

Does implementing Serializable make every field serializable?

No. Every non-transient field's value must itself be serializable, or be null, when writeObject reaches it. Fields marked transient are skipped and come back as default values after deserialization. Static fields are never serialized, because they belong to the class.

Is a marker interface inherited by subclasses?

Yes. If Customer implements Serializable, every subclass of Customer is also Serializable, whether it wants to be or not. A subclass that cannot be serialized safely has to block it explicitly, for example by throwing from a private writeObject method. Annotations, by contrast, are not inherited unless marked @Inherited, and then only from superclasses.

What other marker interfaces exist in the JDK?

java.util.EventListener is a marker for listener interfaces, and java.rmi.Remote marks interfaces whose methods can be called remotely. Spring also uses markers, for example Aware as the parent of callback interfaces such as ApplicationContextAware.

7Conclusion

In this answer, we saw that a marker interface has no methods and works as a label other code checks. ObjectOutputStream throws NotSerializableException for any object in the graph whose class is not Serializable, Object.clone throws CloneNotSupportedException unless the class implements Cloneable, and RandomAccess lets algorithms choose indexed loops for lists with fast get. Markers define a type, so they can be checked at compile time, while annotations carry data and flexibility but are checked only by reflection.

What to say in the interviewAbout 90 seconds aloud

Open with

A marker interface is an interface with no methods. A class implements it to label itself as having some property, and other code checks the label with instanceof and changes its behaviour.

Then, if they want more

The classic JDK examples are Serializable, Cloneable and RandomAccess. With Serializable, ObjectOutputStream checks every object it reaches in the graph; if a class doesn't implement it, writeObject throws NotSerializableException naming that class. The opt-in exists because serialization exposes private state and bypasses constructors, so a class should agree to it deliberately.

With Cloneable, the check is inside Object.clone: it makes a field-by-field copy only if the class implements Cloneable, otherwise it throws CloneNotSupportedException. Cloneable doesn't even declare clone, so the class still has to override it to make it usable, which is why people prefer copy constructors.

RandomAccess marks lists like ArrayList where get by index is constant time, so algorithms such as Collections.binarySearch use an indexed loop for those and an iterator for something like LinkedList. Compared with annotations, the big advantage of a marker is that it defines a type, so a method parameter typed Serializable lets the compiler reject unmarked objects.

Annotations are better when the label needs attributes, applies to methods or fields, or is metadata for a framework, but they can only be checked at run time by reflection.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a marker interface? Give JDK examples and explain how Serializable and Cloneable...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/b9677d0d4b/