<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="core"><font size="2">class X
{
void print (X x){System.out.print("X");}
}
class Y extends X
{
void print (Y y){System.out.print("Y");}
}
class Z extends Y
{
void print (Z z){System.out.print("Z");}
}
class B{
public static void main(
String[] args) {
X x2 = new Y();
x2.print(new Y());
X x3 = new Z();
x3.print(new Z());
}
}</font></pre><hr></blockquote>
The above code prints "XX". I expected it to print "YZ"
What i have understood from K&B is
> at compile time x2.print will chk if print method is present(and accessible) in class X. If not present it will give compile time error.
> At runtime
polymorphism takes place and the method in class Y is invoked.
Can someone tell me if i have understood it right? If yes, then how does the above code print XX?
I faced the problem while solving Whizlabs6 .. code modified by me.
[ July 17, 2008: Message edited by: Milan Sutaria ]