In the code below I have the following problem:
I don't get the semantics of
One hybrid = new Two();
My understanding is that Java performs in this case a so called upcasting.
That even if hybrid
is declared as an instance of One
it the compiler casted it to a Two
instance.
Is this correct?
Therefore at #3 it calls the method foo
of Two
. So far so good.
At #5 things are getting mysterious for me:
Why in earth is the compiler calling the bar
method of One
instead of Two
? What am I missing here?
Thanks
Marcel
class One {
public One() {}
public String foo() {return "One-foo";}
public String bar(One o) {return "One-bar " + o.foo();}
};
class Two extends One {
public Two() {}
public String foo() {return "Two-foo";}
public String bar(Two t) {return "Two-bar " + t.foo();}
};
class Main {
public static void main(String[] args) {
One one = new One();
Two two = new Two();
One hybrid = new Two();
System.out.println(one.foo()); // #1 One-foo
System.out.println(two.foo()); // #2 Two-foo
System.out.println(hybrid.foo()); // #3 Two-foo
System.out.println(one.bar(two)); // #4 One-bar Two-foo
System.out.println(hybrid.bar(two)); // #5 One-bar Two-foo
System.out.println(two.bar(hybrid)); // #6 One-bar Two-foo
}
}
I would expect the following output compared to the one below:
One-foo
Two-foo
Two-foo
One-bar Two-foo
Two-bar Two-foo
Two-bar Two-foo
The actual output:
One-foo
Two-foo
Two-foo
One-bar Two-foo
One-bar Two-foo
One-bar Two-foo