File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why do static method hiding follow Overriding rules? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why do static method hiding follow Overriding rules?" Watch "why do static method hiding follow Overriding rules?" New topic
Author

why do static method hiding follow Overriding rules?

Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
hi all
As we know static methods are never Overridden. Rather they are just redefined.
However I noticed something I could not understand.



If the eat method in Fruit class is just a redefined method and not an Override,
how come compiler complains with this error:

D:\Java Programs>javac Food.java
Food.java:7: eat() in Fruit cannot override eat() in Food; attempting to assign
weaker access privileges; was public
private static void eat(){
^
1 error


Does that mean the Rules of Overriding apply here?

I also tried other violations of the rules and each result in an error related to Overriding.

One more thing,
when i removed static modifier from one of the methods, It also results in an error that too related to Overriding.


D:\Java Programs>javac Food.java
Food.java:7: eat() in Fruit cannot override eat() in Food; overridden method is static
private static void eat(){
^
1 error

please help.


Thanks & Regards

Sudhanshu Gupta

Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Well that is Inheritance? As you are trying to inherit the method from the Parent class so you cannot reduce the visibilty of the method.

Where as Overriding is Polymorphism. Which applies to the instance Objects at Runtime.

I think that clears your doubt.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Sudhanshu Gupta wrote:Does that mean the Rules of Overriding apply here?


No it means the compiler produces a confusing message.

A static method of a superclass can be inherited and hidden, but never overridden.
Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
Thank you both for such a prompt response.
Now my confusion is clear.
Damn java compiler for generating such confusing messages.
Why dont they correct it?

anyways thanks again.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Sudhanshu Gupta wrote:
Why dont they correct it?


I don't know. Another example is "Null Pointer Exception". Java doesn't have pointers. It has references. Still this message is never corrected.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why do static method hiding follow Overriding rules?