• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hiding static methods (confusing)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, have a look at this code:
public class StaticInherit {
public static int num=100;

public static int myMethod() {
return num=num+10;
}

public static void main(String args[]) {
B myB= new B();
System.out.println(myB.myMethod());
}
}

class B extends StaticInherit {

public static int myMehtod() {
return num=num+80;
}
}

OUPUT:
110

How come the output is 110? According to this article Hiding Vs Overriding, for static methods, the compiler only uses the declared type of the reference. That's what we mean when we say a static method does not have run-time polymorphism.

In above example, the reference type is B i.e. B myB= new B(); Then why is compiler invoking superclass version of the method? Thanks
 
Author
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it because you have a typo in the method name in class B?
myMehtod instead of myMethod?
 
Nomi Malo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter, you are da man!!!
 
Nomi Malo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter, you are da man!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic