0
class HelloWorld{

    public static void main(String []args){
        A j = new B();
        B k = new B();

        System.out.println(j.foo(k));
    }
}

class A {
public int foo(A p) { return 1; }
}
class B extends A {
public int foo(A p) { return 2; }
public int foo(B p) { return 3; }
}

So the output printed would be 2. I don't understand how this is so. Would the j object not be seen as its compiled form: A ? Yet it executes the class B version of foo using an A type parameter even though k is of class B at both compile and run time. Why is this so?

tsquared
  • 99
  • 2
  • 14
  • Can you provide a [mcve] - with focus on **minimal**? Class C and local variable i, l and m are irrelevant to your question. (And confusing to people) – Erwin Bolwidt Oct 22 '16 at 14:18
  • I apologize for that. I created C and the other variables for testing purposes but weren't currently using them. I've since made an edit with only useful information. – tsquared Oct 22 '16 at 14:22
  • 1
    Since you overwrote `foo(A p)` wihtin `B`, you cannot call `foo(A p)` from `A` on an object, that has the runtime- (or dynamic-)type ( `B` (variable `j` has static type `A` and runtime-type `B`). But since you access `j` through static type `A`, you can only call methods defined within `A`, thus `foo(B p)` is not accessible. This is why `foo(A p)` of class `B` is called. – Turing85 Oct 22 '16 at 14:25
  • 2
    The key to understanding this case is to appreciate that Java resolves method signatures at compile time and method implementation at runtime. j is declared as type A, and A does not have a method on it of type 'foo(B)'. However B is assignment compatible with A, thus at compile time it is selecting foo(A). At runtime it is thus scanning an object of type B for a method with signature foo(A). Even though B does have a method called foo(B), it will never look at that because of the decisions made at compile time. – Chris K Oct 22 '16 at 14:27
  • 1
    This question has been marked as a duplicate with another question that is broader than this question and it is difficult to pull out a clear answer for this question from the referenced question. – Chris K Oct 22 '16 at 14:31
  • Ah okay thanks to the both of you! I don't think I completely understood how compile time selection works! If `A` theoretically had `foo(B)` method, I'm guessing it would be able to choose `B`s version `foo(B)`? – tsquared Oct 22 '16 at 14:32
  • @Taylor that is right. Give it a go, I predict that it will change from printing 2 to 3. – Chris K Oct 22 '16 at 14:34
  • @ChrisK Perfectly clear. Broader questions are better duplicate target, because they are broader and explain the problem better. This is the concept of canonical question on Stack Overflow. – Tunaki Oct 22 '16 at 14:34
  • The following SO question provides a description of compile time vs runtime resolution. http://stackoverflow.com/questions/1572322/overloaded-method-selection-based-on-the-parameters-real-type – Chris K Oct 22 '16 at 14:38

0 Answers0