3

I was writing my android libraries. I have an interface called IActivity, I have many methods in this interface.

Now, I do not use couple methods in this interface, seems really unnecessary. I deprecated them. However, this is a shared library for may apps. All my apps started the complain.

Is there a way to a deprecation of a method in an interface which will prevent implementation of the method (but also possible to override for old apps) for future applications ?

LazyX
  • 177
  • 2
  • 10

4 Answers4

9

Deprecation of a method in an interface (by using @Deprecated) does not prevent methods from being implemented in classes. It does not generate a compiler error in the class implementing the deprecated method. It simply lets the class author know (at compile time) that this method is deprecated as of this moment and also that it should be avoided if possible (as it may be removed in the future). In fact it does not even generate a warning when the class author is overriding the method but only when somebody is trying to call the method. This makes sense because if the class author does not override the method, the class will remain abstract.

To see this warning though you need to have a reference of the interface type and not of the implementing class type.

Try this example, it should make it clearer.

interface Testable {

    @Deprecated
    void test1();

    void test2();

}

public class Test005 implements Testable {

    @Override
    public void test1(){
        System.out.println("hello 1");
    }

    @Override
    public void test2(){
        System.out.println("hello 2");
    }

    public static void main(String[] args){
        Test005 tA = new Test005();
        tA.test1(); // no warning
        tA.test2(); // no warning

        Testable tB = new Test005();
        tB.test1(); // deprecation warning
        tB.test2(); // no warning
    }
}
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • 1
    How about deprecating a whole interface (should you add `@Depracted` to the methods defined by the interface or add `@Depracted` before the interface ) Thanks – Bionix1441 Mar 15 '17 at 12:21
3

Is there a way to a deprecation of a method in an interface which will prevent implementation of the method (but also possible to override for old apps) for future applications ?

No. But you can simply remove it. Then new classes based on the updated interface won't need to implement it, but old classes based on the older version of the interface won't complain (they'll just have methods that are never used, but that's not a problem in and of itself).

The bigger problem, however, is that if you've changed the interface so much there are methods in it you don't use anymore, is it really likely that older classes implementing the interface will work correctly with your updated code? It doesn't seem likely.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

"way to a deprecation of a method in an interface which will prevent implementation of the method"

No. Cannot be done.

But why not implement a new interface for your newer implementations.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Dhrubajyoti Gogoi
  • 1,265
  • 10
  • 18
0

The recommended approach to deprecating a method (or a class or field) in Java is to use the @Deprecated annotation (since Java 5) or the @deprecated JavaDoc tag (since Java 1.1). Please refer the Oracle documentation on how and when to deprecate APIs that appears to be relevant.

Please note that deprecating any method in this way just lets the programmers know that this method has been deprecated and should be best avoided. There is no actual way to prevent the implementation by deprecating methods.

Yasin
  • 1,906
  • 1
  • 21
  • 37