This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

why do static method hiding follow Overriding rules?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
ScroogeXHTML 9.4 - fast RTF to HTML and XHTML conversion
https://coderanch.com/t/749563/ScroogeXHTML-fast-RTF-HTML-XHTML
reply
    Bookmark Topic Watch Topic
  • New Topic