Tagging Interfaces in Java

前端 未结 8 2246
时光说笑
时光说笑 2021-01-02 06:26

What are tagging interfaces and what are they used for?

8条回答
  •  青春惊慌失措
    2021-01-02 06:57

    You can tag your class with a tagging interface to say to your fellow developer and consumer of your class that you explicitely support that functionality. Think of Serializable; someone who needs to persist a Session and uses serialization to do that can safely use an object of your class.

    It can be further used in reflection; nowadays it is common to use annotations to do this but in the olden days you can inspect a class, check whether it implements a certain interface (like DAO) and if so, process the object further (I'm thinking about the Entity annotation here).

提交回复
热议问题