Mookie Wilson
Mookie Wilson

Reputation: 198

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() {
       ...
    }

    ...
}

Upvotes: 12

Views: 8088

Answers (4)

Senthil Arumugam SP
Senthil Arumugam SP

Reputation: 1529

If we want to refactor the existing code with inappropriate methods in the interface and in the implementation, then we can use @Deprecated in the interface methods in favor of clean new methods temporarily for few releases. It may be ugly, just to keep the code backward compatible we can make use of it. This will show in the IDE and SONAR report that its a deprecated method and forcing the clients to use new methods.

Upvotes: 0

ethan.eldridge
ethan.eldridge

Reputation: 124

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.

Upvotes: 10

Mr. Shiny and New 安宇
Mr. Shiny and New 安宇

Reputation: 13908

@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.

Upvotes: 12

dfa
dfa

Reputation: 116372

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

Upvotes: 6

Related Questions