Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method overriding

 
Ramesh Kale
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.
 
Doyle Matt
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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.
 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read Corey's Tip about this.
 
Doyle Matt
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are never overloaded
maybe you mean "static methods are never overridden"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic