• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need your help, please

 
kevin jia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,gurus
I hava a quettion as follow:
class Test {
static void show() {
System.out.println("Show method in Test class");
}
}
public class Q2 extends Test {
static void show() {
System.out.println("Show method in Q2 class");
}
public static void main(String[] args) {
Test t = new Test();
t.show();
Q2 q = new Q2();
q.show();

t = q;
t.show();
q =(Q2)t;
q.show();
}
}
I think the answer is:
Show method in Test class
Show method in Q2 class
Show method in Q2 class
Show method in Q2 class
But the actual answer is:
Show method in Test class
Show method in Q2 class
Show method in Test class
Show methos in Q2 class
I think the second call t.show will call Q2 version of show() method, I don't know why it call Test version.
Need your help.
 
Uvnik Gupta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin,
The show() methods are declared static and hence they would have static linkages. I mean that the method to be called WILL BE DETERMINED AT COMPILE TIME. Hence it won't show polymorphic behaviour.
Uvnik
 
kevin jia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Uvnik. Now I'm clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic