• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Static overriding

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is in relation to a questiuon by someone earlier.
Someone agreed in saying that you cannot override static methods.
They said that the code below is actually hiding the static method from the supere class.

So -- in this scenario in practice what is the difference between hiding a static method as opposed to the effect of overriding a method generally?

class Test1
{
static void go()
{
System.out.println("Hello, go in super class");
}
}

public class Test extends Test1
{
public static void main(String [] args)
{
Test t = new Test();
t.cgo();
go();
}
static void go()
{
System.out.println("go overridden");
}
void cgo()
{
super.go();
}

}
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this link and scroll down to 8.4.6.2 Hiding (by Class Methods)
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*try this code out to clear it all up */

class sup
{
static void xyz(){
System.out.println("Static Super");

}
void xyz(int a)
{
System.out.println("Instance Super");
}
}
class sub extends sup
{
static void xyz(){
System.out.println("Static Sub");
}
void xyz(int a)
{
System.out.println("Instance Sub");

}
}
class test{

public static void main(String args[]) {
sup a=new sub();
a.xyz();
a.xyz(1);
}
}

The output that you shud get
Static super
Instance sub
 
reply
    Bookmark Topic Watch Topic
  • New Topic