Tag Interface

Category: JavaIdioms


A Tag Interface is a Java term. It is an empty interface which a class implements to claim membership in a set. For example, if a class implements the Serializable interface, it is claiming to be serializable -- to be a member of the set of serializable classes. The interface itself declares no methods; it's just a tag.

There is an issue with TagInterfaces. They are most meaningful to the Java VM itself. The Java VM knows to play some special "tricks" with a cloneable or serializable class. A TagInterface is just some kind of boolean flag for a programmer. It's true or false (instanceof operator). It also provides type compatibility with the interface, but since the interface doesn't declare any methods, there is nothing useful which can be implemented to support the interface.


TagInterfaces are often useful when used in combination with Reflection. As described above, they are a way of associating boolean "flags" with classes or interfaces. These flags can be checked by reflective code written to perform generic operations on any object, such as methods of base classes that FallBackOnReflection. This is how the Serializable interface is used, for example.

Here's another example, from a network protocol framework. A protocol stack is divided into protocol layers, each of which provides interfaces for data transmission and reception, and can also provide one or more control interfaces. The application using the stack, can query for control interfaces by passing the Class object representing the required type of control interface to a query method of the protocol layer at the top of the stack.

A base class is provided to make it easier to implement protocol layers. The base class falls back on reflection to find control interfaces. It enumerates interfaces implemented by the concrete layer class to find those that extend a TagInterface used to identify control interfaces and are the compatible with the control interface required by the application.

Therefore, all protocol control interfaces must be defined to extend the TagInterface. This both helps the implementation of the reflective base class, and stops applications querying for concrete interfaces or classes that are not control interfaces.

-- NatPryce

This is how the JavaBeans framework determines which methods represent an event source. Reflection is used to find methods named addXXXListener( XXXListener l ) and removeXXXListener( XXXListener l ), where XXXListener is an interface that extends the empty TagInterface java.util.EventListener.


A TagInterface is used as a boolean predicate when it is not possible to define that predicate as a method because the objects are not related by inheritance, or because you cannot add methods to a common base class.

The drawback with a TagInterface is that it defines predicate upon instances of a class and all classes derived from that class. A tag interface does not allow that predicate to be evaluated dynamically, or to be overridden in derived classes. This can cause problems later when you want to add such features.


I HaveThisPattern. -- DafyddRees

What do you use it for?


With the advent of AttributedProgramming? a.k.a. AnnotationsInJava?, TagInterfaces might be a thing of past.

An example:

    @Serializable()
    public class MyClass {
    }

-- VhIndukumar
EditText of this page (last edited April 23, 2004)
FindPage by browsing or searching

This page mirrored in JavaIdioms as of April 29, 2006