Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Overriding static methods - pl explain!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Try this.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic