views:

244

answers:

2

Can anybody explain in detail the reason the overloaded method print(Parent parent) is invoked when working with Child instance in my test piece of code?

Any pecularities of virtual methods or methods overloading/resolution in Java involved here? Any direct reference to Java Lang Spec? Which term describes this behaviour? Thanks a lot.

public class InheritancePlay {

    public static class Parent {        
        public void doJob(Worker worker) {
            System.out.println("this is " + this.getClass().getName());

            worker.print(this);
        }
    }

    public static class Child extends Parent {
    }

    public static class Worker {
        public void print(Parent parent) {
            System.out.println("Why this method resolution happens?");
        }

        public void print(Child child) {
            System.out.println("This is not called");
        }
    }

    public static void main(String[] args) {
        Child child = new Child();
        Worker worker = new Worker();

        child.doJob(worker);
    }
}
+3  A: 

The reason is that doJob is implemented in Parent and not overloaded in Child. It passes this to the worker's print methos, because this is of the type Parent the method Worker::print(Parent) will be called.

In order to have Worker::print(Parent) called you needto overload doJob in Child:

public static class Child extends Parent {
    public void doJob(Worker worker) {
        System.out.println("from Child: this is " + this.getClass().getName());

        worker.print(this);
    }
}

In the code above this.getClass() in Child is equivalent to Child.class.

rsp
rsp, thanks for pointing this, but I know that overriding doJob in Child makes prog work.The thing is that i don't understand why :).Let's take a look at the initial code.When we pass Child object to Parent.doJob Java reflection inside Parent.doJob proves that we deal with this of type Child, so why does the resolution of overloaded method fails?
Max
@Max, inside a method the type of `this` is always the type of the class the method is in. The compiler cannot know that the class is going to the inherited from in the future, it has to use the knowledge it has at that point. `this.getClass()` is runtime information, not compile time.
rsp
rsp, thanks a lot. already got it.
Max
+4  A: 

The JLS states in §8.4.9 Overloading:

  1. When a method is invoked (§15.12), the number of actual arguments (and any explicit type arguments) and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked (§15.12.2).
  2. If the method that is to be invoked is an instance method, the actual method to be invoked will be determined at run time, using dynamic method lookup (§15.12.4).

So in your case:

  1. The method argument (this) is of compile-time type Parent, and so the method print(Parent) is invoked.
  2. If the Worker class was subclassed and the subclass would override that method, and the worker instance was of that subclass, then the overridden method would be invoked.

Double dispatch does not exist in Java. You have to simulate it, e.g. by using the Visitor Pattern. In this pattern, basically, each subclass implements an accept method and calls the visitor with this as argument, and this has as compile-time type that subclass, so the desired method overloading is used.

Christian Semrau
Christian, thanks for exhaustive answer!So we deal here with runtime VS compiletime types thing. I'll dig much into that topic.(Double dispatch is mentioned here because I ran across this question learing the very Visitor pattern :) ).Max
Max