views:

266

answers:

3

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

    ...
}
+2  A: 

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

dfa
I'd go further than controversial: I'd say it's questionable. I'd certainly ask for it to be justified in a code review.
CPerkins
Why not? That simply means that one method in the multi-method interface is planned to be removed in the future releases.
eugener
@Eugene: That may be true, but the example only showed one method. So, why even have that interface if it only has one method? :/ lol
Zack
@Eugene: Sometimes an interface is used as a marker and contains no methods, such as Serializable. If Serializable contained a method that was now deprecated it would still be a useful interface and Serializable objects should still implement that method if they expect to be compatible with any old clients.
Mr. Shiny and New
@dfa, @CPerkins: A class should implement all the methods of its interfaces for compatibility with the clients that use them; if the Joe interface has any use besides that deprecated method then the deprecated method should be implemented.
Mr. Shiny and New
a class **must** implement all the methods, no way :)
dfa
@dfa I meant implement the method properly, as opposed to a stub that's a no-op.
Mr. Shiny and New
+1  A: 

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

Mr. Shiny and New
A: 

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.

ethan.eldridge