jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need your help, please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need your help, please" Watch "Need your help, please" New topic
Author

Need your help, please

kevin jia
Ranch Hand

Joined: Jul 13, 2000
Posts: 38
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

Joined: Jul 24, 2000
Posts: 32
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

Joined: Jul 13, 2000
Posts: 38
Thanks Uvnik. Now I'm clear.
 
 
subject: Need your help, please
 
Similar Threads
plz help
plz help
Tips,oneliner Just before the SCJP Exam
Question
Sub Class and super class