Overriding a Deprecated Function

Overriding a Deprecated Function

I have a class that extends one class and implements an interface.The interface requires a getName() method.At the same time the class I am extending has a getName() functionthat has been deprecated. Can I override this collision and provide agetName() method to satisfty the interface?

You may override the method and also have the method implement theinterface. However, the Java compiler will complain about overridinga deprecated method, producing a deprecation warning. You cancompile the following code samples to test this out.

Be sure tocompile the classes in the deprecation package first and to place thesource files somewhere outside of the compiler’s classpath when youcompile If you don’t do this, the compiler will findthe source files during compilation and not emit any warnings. TheJDK compiler only appears to recognize deprecation in a class file.

import deprecation.*;public class Deprecation {  class DerivesAndImplements extends Deprecated implements GetName {    public String getName() { return "DerivesAndImplements"; }  }  public void test() {    Deprecated deprecated = new Deprecated();    Derives derives = new Derives();    DerivesAndImplements dai = new DerivesAndImplements();    GetName getname = dai;    System.out.println(deprecated.getName());    System.out.println(derives.getName());    System.out.println(dai.getName());    System.out.println(getname.getName());  }  public static void main(String[] args) {    (new Deprecation()).test();  }}package deprecation;public class Deprecated {  /**   * @deprecated This method is deprecated.   */  public String getName() { return "Deprecated"; }}package deprecation;public class Derives extends Deprecated {  public String getName() { return "Derives"; }}package deprecation;public interface GetName {  public String getName();}
See also  Essential Measures for Safeguarding Your Digital Data

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist