Tagging Interfaces in Java

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

What are tagging interfaces and what are they used for?

8条回答
  •  一个人的身影
    2021-01-02 06:43

    Because sometimes, it really makes sense if some property of a type can be used as a type itself - Serializable comes to mind. If I make a method like this:

    public void save(Object data){ ... }
    

    ... you don't really know how that data will be saved. VM serialization? Bean property serialization? Some homebrewed scheme? Whereas if you write it like this:

    public void save(Serializable data){ ... }
    

    ... it is quite clear (if only the designer of ObjectOutputStream had used this possibility!). Sometimes it makes sense to use annotations when you want to add meta-data to types, but in this case, I'd argue for a tagging interface.

提交回复
热议问题