• 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

Static method

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to HIDE a static method in a superclass in the following code:
class suupcls{
public static void mthd(){
System.out.println("This supercls static mthd");
}
class subcls extends supcls{
public static void mthd(){
System.out.println("This subcls static mthd");
}
}
public class test{
public static void main(String args[]){
subcls.mthd();//calls supercls mthd
subcls subob=new subcls();
subob.mthd();//ALSO CALLS MTHD IN SUPERCLS
}
}
thanx in advance
rishi
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Try this
-------------------------------------------
class supcls{
public static void mthd(){
System.out.println("This supercls static mthd");
}
}
class subcls extends supcls{
public static void mthd(){
System.out.println("This subcls static mthd");
}
}
public class test{
public static void main(String args[]){
subcls.mthd();//calls subcls mthd
subcls subob=new subcls();
subob.mthd();//ALSO CALLS MTHD IN SUBCLS
}
}
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this code, had to fix some braces on it, but the subclass method is called as expected. The reference determins which static method to call.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code and the output, both print the subclass

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't understand the question fully.I guess question is regarding which method is invoked while calling overriden static methods.
To call static overriden methods , compiletime reference type is used .Coz static methods are not overriden.In the following code,
in line 14, compiletime reference type is supcls ,so super class method is executed.In line 15 though sup1 object type is of subcls,the compiletime reference type is supcls,so again supcls method is executed.But in line 16 since compile time reference type of sub1 is subcls ,subclass method is called.
Try the following code.

o/p is
This supercls static mthd
This supercls static mthd
This subcls static mthd
Hope that helps.
Veena
[ June 25, 2003: Message edited by: Veena Point ]
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic