Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

A question from

Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic