Ernest Friedman-Hill wrote:
Hmmm. I suppose the Eclipse warning is the key
22.214.171.124 Overriding (by Instance Methods)
An instance method m1 declared in a class C overrides another instance method, m2, declared in class A iff all of the following are true:
C is a subclass of A.
The signature of m1 is a subsignature (§8.4.2) of the signature of m2.
m2 is public, protected or declared with default access in the same package as C, or
m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.
Moreover, if m1 is not abstract, then m1 is said to implement any and all declarations of abstract methods that it overrides.
Ireneusz Kordal wrote:
@Override is not the Eclipse warning, this is Java warning;
Steve Luke wrote:...
So when you call top.foo() and Top#foo() is visible to the caller, Top#foo() gets called. I didn't find anywhere in the JLS that states that, given two visible methods for a type in scope which one will be called but it makes sense the one would be the reference-type method, but I can't find documentation to back me up.
15.12.1 Compile-Time Step 1: Determine Class or Interface to Search
The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. There are several cases to consider, depending on the form that precedes the left parenthesis, as follows:
- If the form is MethodName, then there are three subcases:
- - If it is a simple name, that is, just an Identifier, then the name of the method is the Identifier. If the Identifier appears within the scope (§6.3) of a visible method declaration with that name, then there must be an enclosing type declaration of which that method is a member. Let T be the innermost such type declaration. The class or interface to search is T.
- - If it is a qualified name of the form TypeName . Identifier, then the name of the method is the Identifier and the class to search is the one named by the TypeName. If TypeName is the name of an interface rather than a class, then a compile-time error occurs, because this form can invoke only static methods and interfaces have no static methods.
- - In all other cases, the qualified name has the form FieldName . Identifier; then the name of the method is the Identifier and the class or interface to search is the declared type T of the field named by the FieldName, if T is a class or interface type, or the upper bound of T if T is a type variable.
15.12.4 Runtime Evaluation of Method Invocation
126.96.36.199 Locate Method to Invoke
The strategy for method lookup depends on the invocation mode.
Otherwise, the invocation mode is interface, virtual, or super, and overriding may occur. A dynamic method lookup is used.
The dynamic lookup process starts from a class S, determined as follows:
- If the invocation mode is interface or virtual, then S is initially the actual run-time class R of the target object. This is true even if the target object is an array instance. (Note that for invocation mode interface, R necessarily implements T; for invocation mode virtual, R is necessarily either T or a subclass of T.)
- If the invocation mode is super, then S is initially the qualifying type (§13.1) of the method invocation.
The dynamic method lookup uses the following procedure to search class S, and then the superclasses of class S, as necessary, for method m.
Let X be the compile-time type of the target reference of the method invocation.
- If class S contains a declaration for a non-abstract method named m with the same descriptor (same number of parameters, the same parameter types, and the same return type) required by the method invocation as determined at compile time (§15.12.3), then:
- - If the invocation mode is super or interface, then this is the method to be invoked, and the procedure terminates.
- - If the invocation mode is virtual, and the declaration in S overrides (§188.8.131.52) X.m, then the method declared in S is the method to be invoked, and the procedure terminates.
- - Otherwise, if S has a superclass, this same lookup procedure is performed recursively using the direct superclass of S in place of S; the method to be invoked is the result of the recursive invocation of this lookup procedure.