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 ?
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With