File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question from sarga.com 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 "A question from sarga.com" Watch "A question from sarga.com" New topic
Author

A question from sarga.com

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
consider the following code
1 class Base
2 {
3 static void test() {
4 System.out.println("Base.test()");
5 }
6
7 }
8
9 public class Child extends Base {
10
11 static void test() {
12 System.out.println("Child.test()");
13 }
14
15 static public void main(String[] a) {
16 Child anObj = new Child();
17 anObj.test();
18
19 Base baseObj = anObj;
20 baseObj.test();
21 }
22 }
According to my understanding, At line # 19 , I stored child object reference as Base object reference.So,at line#20,it should call child class's test method.
But it is calling base class 's test method only.
Can somebody explain this?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Vamsi,
This has to do with early Vs. late binding. Static references are resolved at compile time, instead of runtime. Infact, the same thing happens with instance variables too, which are resolved at compile time. Just like static methods, instance variables are not polymorphic.
If the test method in Base and Child were to be non-static, then it would have displayed the polymorphic behaviour calling the Child.test() method in both the calls. Just comment out the static keyword for both test methods and see what happens!!
Good luck.

Ajith
[This message has been edited by Ajith Kallambella (edited May 15, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question from sarga.com
 
Similar Threads
Garbage collection..
Casting question
Overriding
Another mock exam question.
call to shadowed static method at runtime!!!