Interface Tags

These are sometimes called "Tag Interfaces". It's an interface that defines no methods. Often used to distinguish between things that all implement a particular method, but where the method may not be accessible to all. For instance, Cloneable is a Tag Interface -- clone() is implemented in Object, but only classes that implement Cloneable can use it. --KyleBrown


I regard such things with suspicion. In the case of Cloneable, it seems to be a cludge to cover a number of Java short-comings. It would make sense to put the clone() method into Cloneable rather than Object, but because Java doesn't support true multiple inheritance there would be nowhere to put the implementation. -- DaveHarris


Another advantage of "Tag Interfaces" is that the properties of an object can be queried by examining it's Class *before* any instances have been created. This can be used to to find an appropriate class to instantiate, for example.

The alternative approaches are to define accessor methods that describe the properties of the object or to describe the class in a separate properties file. The disadvantage of the former approach is that the object must be instantiated before it can be examined, which could consume too much time or resources. The latter approach duplicates information in several places (the source code and the properties file) and one must also ensure that the properties file can be loaded wherever the class is used, which means including it in a JAR file or making it available on the web server if it is to be used by applets. --NatPryce


The more common term for this inside Sun is ``marker interface''. I have always found them suspicious, but occasionally the correct thing. The Serializable marker interface (in java.io) is reasonable to mark that the persistent state of the object is not going to be actually private. The java.rmi.Remote interface is useful in a similar way, because it marks the object as being handled differently on remote calls. Such marker interfaces are pure semantics. It is rare that something in a programming context has only non-language contractual semantics, but when it happens, marker interfaces are a reasonable way to indicate it. -- KenArnold


CategoryInterface


EditText of this page (last edited June 19, 2002) or FindPage with title or text search