Course contentsShow
← Java
Lesson 494 of 2,87012. Abstract Classes and InterfacesFree lesson

Empty Interfaces: Marker Interfaces

Exploring marker interfaces like Serializable that contain no methods but signal a capability or contract.

Empty Interfaces: Marker Interfaces

What you'll learn: You'll discover how interfaces with no methods at all can still serve as powerful type markers in Java.

The Paradox of Empty Interfaces

So far, you've seen interfaces define methods that classes must implement. But what about an interface with no methods at all? Surprisingly, these marker interfaces are entirely valid and useful in Java.

A marker interface is simply an empty interface that acts like a label or tag. When a class implements a marker interface, it's making a declaration: "I have a certain capability" or "I opt into a particular contract." The interface doesn't dictate what to implement—it just marks the class as having a special characteristic.

The Classic Example: Serializable

The most famous marker interface in Java is java.io.Serializable:

public interface Serializable {
    // Completely empty!
}

When a class implements Serializable, it's signaling to the Java runtime: "My objects can be converted to bytes and saved or transmitted." The interface itself requires no methods, but other parts of Java check for this marker using instanceof:

class User implements Serializable {
    String name;
    int age;
}

// Later, the JVM can check:
// if (obj instanceof Serializable) { /* allow serialization */ }

Why Not Just Use a Method?

Marker interfaces provide a type-level guarantee checked at compile time. It's cleaner than documentation or runtime flags, and the implements clause makes the capability explicit in the class declaration.

Key Takeaway: Marker interfaces contain no methods but serve as type labels that signal a class's capability or opt-in to a special contract, enabling compile-time type checking and runtime behavior based on instanceof tests.