Can anyone tell me if this code:
public class OvTester {
@Override
public int hashCode() {
return toString().hashCode();
}
}
The @Override annotation does not "determine" anything. It is simply a flag that tells the compiler to raise an error if the annotated method is not overriding a superclass or interface method. It's a tool for developers to help maintain their sanity, and nothing more.
In this specific case, it is simply noting that the hashCode() implementation in OvTester is overriding the hashCode() method defined in Object. This has nothing at all to do with toString(), and calling the superclass's toString() method from your hashCode() method will not/is not the same thing as overriding toString().
is this true? the hashCode() method in OvTester must override the same name method in its superClass?
That is indeed true, in the sense that the annotation will cause the compiler to raise an error if there is not an overridable hashCode() method in the superclass that matches the signature of the annotated method.