• 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overriding static methods - pl explain!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am BaseClass");
}
}

public class MyFor extends BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am SubClass");
}

public static void main(String [] arg){
BaseClass bc = new MyFor();
bc.sayHello();
}
}
I thought that the answer for the above is "Hi pal!!!, I am SubClass" but the answer which is given is "Hi pla!!!, I am BaseClass". Can anybody explain please?Thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static and overloaded method invokation is decided at compile time and not at run time.
So even if the object is of subclass type, the method invokation is dependent on reference.
 
reply
    Bookmark Topic Watch Topic
  • New Topic