I have the following codes.
public class Parent {
@Override
public int hashCode() {
return 0;
}
}
public class Child extends Parent {
public void test() {
this.toString();
this.hashCode();
}
}
As you can see in the above codes, Child inherits toString () from Object and hashCode () from the parent. The byte code of the Child # test is as follows.
ALOAD 0: this
INVOKEVIRTUAL Object.toString() : String
ALOAD 0: this
INVOKEVIRTUAL Child.hashCode() : int
RETURN
I think that if invokevirtual calls Object.toString (), it should call Parent.hashCode () for consistency. or, called by Child.hashCode (), then you need to call Child.toString ().
However, invokevirtual does not maintain its consistency if and only if the target method is inherited by Object.
Only in this case does the invokevirtual method call on Object. For other cases, the invokevirtual calls method in the current class.
I want to know why this is happening.