Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecate final super class method?

Tags:

java

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.

like image 997
Kilian Avatar asked Sep 09 '18 07:09

Kilian


People also ask

How do you deprecate a method in Java?

Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead. Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used.

What does deprecated annotation do?

The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is.

How do you invoke an overridden superclass method from a subclass?

It is what overriding for. The overridden method shall not be accessible from outside of the classes at all. But you can call it within the child class itself. to call a super class method from within a sub class you can use the super keyword.

Which method overrides a method in the superclass?

Instance Methods An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.


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.

like image 192
Isfirs Avatar answered Oct 21 '22 01:10

Isfirs