aspose file tools*
The moose likes Beginning Java and the fly likes static methods-overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static methods-overriding" Watch "static methods-overriding" New topic
Author

static methods-overriding

Tatiana Ivanova
Greenhorn

Joined: Jul 30, 2010
Posts: 3
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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Nov 04, 2009
Posts: 2066

static methods are called on reference variable, not on the actual object.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

I dont think there are restrictions that static methods should not/cannot be overridden...Is there actually any??


OCPJP 6
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
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

Joined: Aug 19, 2009
Posts: 276

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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
We also have a JavaRanch FAQ about that.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 19, 2009
Posts: 276

Got that difference between overriding and hiding...
Thanks to Campbell for that FAQ page! Was clear as a crystal!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
You're welcome
Tatiana Ivanova
Greenhorn

Joined: Jul 30, 2010
Posts: 3
thank you very much for all your replays, I finally got to understand it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods-overriding