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

static method overriding

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the example i got from dans exam

class E {
void printS1(){System.out.print("E.printS1 ");}
static void printS2() {System.out.print("E.printS2 ");}
}
class F extends E {
void printS1(){System.out.print("F.printS1 ");}
static void printS2() {System.out.print("F.printS2 ");}
public static void main (String args[]) {
E x = new F(); x.printS1(); x.printS2();
}
}

The result is
F.printS1 E.printS2
---
Any one have any idea about the static method overriding. I think static method always call on reference.
Please correct me... if I am making mistake.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you're right. static method calls are determined by the type of reference they have and not the object that the reference contains.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods are never overloaded, what your doing is called hiding. You can do the same with variables as well(rules are different). So basically you can use the refereance type to access the hidden method.
however if you did this F x = new F(); and did x.printS2() the output would be F.printS2.



Here is more about hiding

variable hiding
method hiding
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read Corey's Tip about this.
 
Doyle Matt
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods are never overloaded
maybe you mean "static methods are never overridden"
 
reply
    Bookmark Topic Watch Topic
  • New Topic