Java – @Override Annotation

annotationsjava

Can anyone tell me if this code:

public class OvTester {
    @Override
    public int hashCode() {
        return toString().hashCode();
    }
}

determines that the toString method in the OvTester class overrides the toString method in its superclass.

I'd like to know if this is true, and if so how it works?

If it's not true, then is this true:

"the hashCode() method in OvTester must override the same name method in its superclass"

?

If that's not correct then what is correct?

Best Answer

Method overriding happens when you redefine a method with the same signature in a sublcass.

So here you are overriding hashCode(), not toString()

The @Override annotation is optional (but a very good thing) and indicates that this is expected to be overriding. If you misspell something or have a wrongly-typed parameter, the compiler will warn you.

So yes, the 2nd statement is true (and the superclass in this case is java.lang.Object)

Related Question