Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to avoid deprecated warning in derived interfaces which override deprecated members?

Consider the following simplified interface inheritence hierarchy:

// Starting point:
public interface Base {
    void Foo();
}

public interface Derived extends Base {
}

It is intended to move the Foo method from the Base interface to the Derived interface:

// Desired end-point:
public interface Base {
}

public interface Derived extends Base {
    void Foo();
}

In order to phase in this breaking change, it is desired to retain backwards compatibility of the Base interface for some time.

This can be achieved by marking the method on the Base interface as @Deprecated:

// Intermediate state:
public interface Base {
    /**
     * @deprecated This method is deprecated as of release X. Derived.Foo should be used instead.
     */
    @Deprecated void Foo();
}

public interface Derived extends Base {
    void Foo(); 
}

When I compile this code I receive a compiler warning for Derived:

[deprecation] Foo() in interface Base has been deprecated

Oddly, if I remove the @deprecated from the documentation in Base (but leave the @Deprecated) this warning disappears.

Is it correct that I get this warning, and if so, how can I work around this?


The warning seems to communicate that Derived.Foo is "using" Base.Foo (which is deprecated). But the only capacity in which Derived.Foo is "using" the deprecated Base.Foo is to override it. That seems to say that you are not allowed to override deprecated interface methods in derived methods.

If this is the case, should I then decorate Derived with @SuppressWarnings("deprecation") to suppress the warning?

like image 997
Daniel Fortunov Avatar asked Jul 31 '09 15:07

Daniel Fortunov


People also ask

How do you stop a deprecation warning in Java?

You can use the @SuppressWarnings annotation to suppress warnings whenever that code is compiled. Place the @SuppressWarnings annotation at the declaration of the class, method, field, or local variable that uses a deprecated API.

Why we should not use deprecated methods in Java?

A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it since it has been superseded and may cease to exist in the future.

What happens when a method is deprecated?

Similarly, when a class or method is deprecated, it means that the class or method is no longer considered important. It is so unimportant, in fact, that it should no longer be used at all, as it might well cease to exist in the future.

How do you deprecate an interface in Java?

Java supports deprecation as a language feature: Deprecate a feature by adding the tag @deprecated to its javadoc documentation.


2 Answers

I believe your requirement is valid, I have no doubt that overriding the deprecated method is the correct way to go.

I believe the difference between @deprecated and @Deprecated is mainly historical. @Deprecated is the official way in java 5, but is new, so we are expected to double it with @deprecated.

Also note that, sadly enough, @Deprecated doesn't let you specify information .. while information is usually needed, for example to tell what should be used as a replacement, or when the deprecated method is expected to be completely removed.

Not knowing more, and knowing the problem will disappear as soon as you effectively remove the super method, I would use the @SuppressWarnings("deprecation"), possibly with a comment for your successors to understand ... (and another comment on the super method to tell them to remove all that when deleting the method). ;-)

like image 169
KLE Avatar answered Oct 13 '22 02:10

KLE


if I understand correctly, you need a @SuppressWarnings("deprecation") at the beginning of your classes that implement the deprecated interface/function. Or am I way off base here?

like image 45
MattC Avatar answered Oct 13 '22 02:10

MattC