Possible 开发者_高级运维Duplicate:
marker interface
How Marker interface works since it has no methods?
Marker interfaces are just that - markers.
They allow code inspection (and other) tools to look at the code and find any class that "implements" the marker interface and do something with it.
This may be the compiler or a third party tool that may change or generate some code depending on the mere existence of the marker interface.
It is not the usual way interfaces are supposed to be used, but in some situations it may be useful.
The only reason why you would do this is if you wanted to store metadata about your class and wanted to use the type checking system to enforce some rules based on the metadata.
For example, with Serializable
interface, you basically say that instances of the class inplementing the interface are serializable, and that's it - you just say it, and leave to some other mechanism to so something about it.
Nowadays, with newer versions of Java we can use more sophisticated mechanism of annotations to do the same thing (although, they are not enforced by type system).
Like a normal interface:) It has typically no methods and it is there just for marking. Let's take for example Serializable
. Before an object is send over the wire, Java
checks if the class implements this marker interface. If not, an exception is thrown. Its purpose is better suited with an annotation.
精彩评论