What have you done? You have declared a variable, assigned an object to it, and called its hashCode method. Write down the inheritance hierarchy of ref, and it should become obvious to you where the hashCode() method comes from. If that doesn't work, try . . . and that should give a hint where the hashCode() method comes from.
Joined: Jul 24, 2008
Output : 1476323068
See "Inter" interface does not have hashCode() method and if you see the "ref" static data type is "Inter".
So the rule says that using interface reference you can call only those methods on the target object which are there in the interface.
But Inter can only be instantiated as a concrete class, and a concrete class will always inherit from Object. Since there is no possible way the call to hashCode() can be invalid, there's no reason for the compiler to see it as an error.
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type "r", and throws clause t corresponding to each public instance method m with signature s, return type "r", and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
In other words, every "root" interface implicitly has the following methods:
- public abstract boolean equals(Object)
- public abstract int hashCode()
- public abstract String toString()
- likewise for the final methods of Object (getClass(), notify(), notifyAll(), wait(), wait(long), wait(long, int)
(note that clone() and finalize() are not implicitly declared; these are protected in java.lang.Object and all methods in interfaces must be public)
These last methods cannot be explicitly redeclared, the first three can.
The reason is because of what Matthew said: in the end, no matter what the concrete class of the actual instance, Object is at the root of the hierarchy.