java – An interface containing no members

Question:

Is a memberless интерфейс possible, and if so, what's the practical point of such an interface?

Answer:

Yes, it is possible. Such interfaces are called marker interfaces. They are needed to indicate something to the JVM, compiler or some library. They are empty because they allow you to add some feature / marker / indication to types without changing the contracts of these types.

In Java, such interfaces are, for example, Serializable and Cloneable . The existence of Serializable allows us to make the check "can this object be serialized" in a more typed way, reducing it to checking "whether this object implements the Serializable interface".

Annotations (for example, @ThreadSafe ) can also serve as replacements for marker interfaces.

Marker interface on Wikipedia.

Scroll to Top