File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question from Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question from" Watch "A question from" New topic

A question from

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 }
7 }
9 public class Child extends Base {
11 static void test() {
12 System.out.println("Child.test()");
13 }
15 static public void main(String[] a) {
16 Child anObj = new Child();
17 anObj.test();
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

Joined: Mar 17, 2000
Posts: 5782
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.

[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:
subject: A question from
It's not a secret anymore!