@override annotation

后端 未结 6 1139
别跟我提以往
别跟我提以往 2021-01-17 11:57

Can anyone tell me if this code:

public class OvTester {
    @Override
    public int hashCode() {
        return toString().hashCode();
    }
}
6条回答
  •  一个人的身影
    2021-01-17 12:27

    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)

提交回复
热议问题