Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Sep 14, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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() function that has been deprecated. Can I override this collision and provide a getName() method to satisfty the interface?

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

Be sure to compile the classes in the deprecation package first and to place the source files somewhere outside of the compiler's classpath when you compile Deprecation.java. If you don't do this, the compiler will find the source files during compilation and not emit any warnings. The JDK 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;

  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();

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date