Deprecate final super class method?

Viewed 444

How would you go about indicating the deprecation of a final method of a superclass?

//Class a out of my control
class A{
    public final void foo(){
     ...
    }
}

class B extends A{
   public void fooAlternative(){...}

   //deprecate foo?
}

Background: When extending the JavaFX API we are faced with several final methods preventing us from making changes as we please. Sometimes this is required and the only suitable solution I have found is creating an additional method. In this scenario deprecating the method provided by A would be great to make the programmer aware that a different alternative exists.

Wrapping the object is not a viable option as inheritance is required for polymorphism.

1 Answers

To give you a clearer example of what Eugene said in the comments:

import javafx.scene.control.Label;

/**
 * Example as described by @Eugene
 */
public class AWrapper {

    private final Label wrapped = new Label();

    public AWrapper() {

    }

    // Implement only what you **want** to expose

    // Assume setText as "deprecated"

    /**
     *
     * @param text The text
     * @see Label#setText(String)
     */
    @Deprecated
    private void setText(final String text) {
        wrapped.setText(text);
    }

    /**
     * Alternative to {@link AWrapper#setText(String)}.
     *
     * Does some extra work.
     *
     * @param text New text
     */
    private void text(final String text) {
        System.out.println("New Label text: " + text);

        wrapped.setText(text);
    }

}

Instead of doing inheritance, you do composition and you will only expose whatever API you need (or the full original if you have the time). This basically gives you control over the "super" element. There is no way to edit meta information on super classes, which you shouldn't edit anyway.

Related