How to override the javadoc for a final method in a sub class?

Jai :
public class BaseClass {
    /**
     * Gets the value.
     */
    public final String getValue() {
        // returns something.
    }
}

public class SubClass extends BaseClass {
    /**
     * Gets the value.
     * <p/>
     * The value is meaningless for SubClass.
     */
    @Override // Cannot override final method
    public final String getValue() {
        super.getValue(); // Not overriding implementation, just javadoc
    }
}

I do not need to change the implementation of the final method, I just want to change the Javadoc for it.

GhostCat salutes Monica C. :

Simply spoken: you can't do that. If at all, you could put some javadoc at the definition of that child class explaining that the behavior of that one final method has been changed. And if you can do that - you could change the javadoc on the base class to say: "sub classes might invalidate this method" or something alike.

Beyond that, you should understand that this is also a questionable idea conception wise.

Inheritance is not primarily about code reuse. It is about expressing that some class A is-a B, because A extends B. So when you decide to make a method meaningless on a subclass, you are basically invalidating the contract of the super class. And that is simply not good practice (see the Liskov substitution principle for why you have to be careful when modifying the contract of inherited methods).

You see, that keyword final is not only information to the compiler. It also expresses intent by the person who used it for that very method. This person said: "I don't want that subclasses temper with this method!"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=473815&siteId=1