• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods-overriding

 
Tatiana Ivanova
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a problem of understanding this example:



The result of compilation of this code is "fa la". I don't understand the result, as I know the static methods can't be overridden that's why I think the result should be "la la".

Please help me to understand this result.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are calling static methods, the compiler generates code as if you coded it as:BTW, this is not method overriding as the two sing() methods are in different classes.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are called on reference variable, not on the actual object.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think there are restrictions that static methods should not/cannot be overridden...Is there actually any??
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods can never be overriden, It is always invoked based on the reference type. In your case "t" has a reference type of "Tenor" so "fa" got printed and "s" has a reference type of "Singer" so "la" got printed

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Reilly wrote:
BTW, this is not method overriding as the two sing() methods are in different classes.


Isn't this called over-riding? Same method with same signature in both the base class and derived class!
This is over-riding,right, irrespective of on which reference you invoke the method?
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:Isn't this called over-riding? . . .
No, it isn't. It's called hiding.

In Bloch and Gafter, Java Puzzlers (page 180) they quote three Java™ Language Specification sections: 8.3, 8.4.8.2 and 8.5.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We also have a JavaRanch FAQ about that.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:
Isn't this called over-riding? Same method with same signature in both the base class and derived class!
This is over-riding,right, irrespective of on which reference you invoke the method?


Did you run the code? If overriding, did you get virtual method invocation feature? Check it. Static methods look like they override the super class one, but, actually the are hiding. If overriding => virtual method invocation will happen in a polymorphic environment(like the situation in the OP's code line 8)!
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got that difference between overriding and hiding...
Thanks to Campbell for that FAQ page! Was clear as a crystal!
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Tatiana Ivanova
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for all your replays, I finally got to understand it.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic