Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are benefits of using the @Deprecated notation on the interface only?

For Java programming, what are some benefits of using the @Deprecated notation on and interface method but not on the class that implements it?

public interface Joe {

    @Deprecated
    public void doSomething();

    ...
}

public final class Joseph implements Joe {

    public void doSomething() {
       ...
    }

    ...
}
like image 979
Mookie Wilson Avatar asked Aug 12 '09 18:08

Mookie Wilson


3 Answers

@Deprecated is documentation. If people code to an interface you can mark certain aspects of that interface as deprecated. That way people know not to use it.

The implementation class of the interface is a detail. A method in that class happens to satisfy the interface but may not be deprecated on its own. Deprecating that method may or may not be appropriate.

Creating a new class that implements an interface means you need to implement the deprecated methods. They should probably work unless you know that the clients of the class don't use the deprecated methods. For example, if you are creating an HTTP servlet container you need to implement the HttpServletResponse.encodeUrl() method even though it's deprecated in favour of encodeURL(). That's because a user of your class may call that deprecated method.

like image 120
Mr. Shiny and New 安宇 Avatar answered Nov 08 '22 02:11

Mr. Shiny and New 安宇


I believe it's a shortcoming in the Java Language itself and it is nonsense to specify a method in an interface as deprecated via an annotation and not have the method considered deprecated in the implementing class.

It would be better if the @deprecated-ness of the method were inherited. Unfortunately, it seems Java does not support this.

Consider how tooling, such as an IDE, treats this situation: If the type of a variable is declared to be the interface, then @deprecated methods can be rendered with a strike through. But if the type of a variable is declared to be the implementing class and the class signature does not include @deprecated, then the method will be rendered without a strike through.

The fundamental question is: what does it MEAN for a method to be deprecated in an interface but not in an implementing class (or in an extending interface)? The only reasonable intention is for the method to be deprecated for everything below the interface in the class hierarchy. But the language does not support that behavior.

like image 10
ethan.eldridge Avatar answered Nov 08 '22 02:11

ethan.eldridge


in my opinion it is controversial: a deprecated method interface should not not be used regardless it's implementation (please provide counterexamples if not)

like image 6
dfa Avatar answered Nov 08 '22 04:11

dfa