Search code examples
javainterfaceannotations

What is a deprecated annotation target - implementation, interface or both?


I wonder, if I have to annotate a @Deprecated method at all their occurrences e.g: Interface, Base (Abstract Class) and all the Implementation Classes or is one annotation in the Interface sufficient?


Solution

  • Actually depends of how deprecated classes are used. Deprecate an interface (methods or attributes) is enough if you use good practices (instantiating the interface to use the implementation).

    Anyway,

    • you cannot ensure / force this practice in other users
    • not all compilers/parsers will throw a warning or show javadoc @deprecated tag

      Using the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element. In contrast, there is no guarantee that all compilers will always issue warnings based on the @deprecated Javadoc tag, though the Sun compilers currently do so. Other compilers may not issue such warnings. Thus, using the @Deprecated annotation to generate warnings is more portable that relying on the @deprecated Javadoc tag.


    Resuming: if you want to deprecate a method implementation you must deprecate both, interface and implementation.

    SOURCES 1 2 3