• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding static method  RSS feed

 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My understanding of static says that static methods cannot be overrriden but the compilation of the above code results in Overriding rule violation error
Why is this so?

Thanks,
Jitendra
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is called method hiding.
More information here http://www.coderanch.com/how-to/java/OverridingVsHiding
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be way off (since I'm only a beginner) but the first thing that jumped at me from your code is the fact that you made the doStuff() method from your subclass protected which as far as I know limits it's scope of visibility compared to the same method from your superclass. So that might be where you are getting an error from.

As for the overriding static methods, far as I know you cannot override them(at least not if you think from the perspective of how an overriden method should work in java) but if you do try to override you should not get a compiler error because of it.

But I am just a beginner so I might be wrong , I have a link bookmarked somewhere about how overriding static methods works in Java, if your problem isn't solved by the time i get back(have to go in like 30 seconds) I will try to find it and post it for you. It was something like "geekexplains blogspot" but i can't be sure.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Vlad wrote: . . . I have a link bookmarked somewhere about how overriding static methods works in Java . . .
Please quote that link, and read it again carefully. As you have already been told, you can't override static methods, so you may have misread the webpage.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said you cannot override them.

This is the link i meant: http://geekexplains.blogspot.com/2008/06/can-you-override-static-methods-in-java.html

When I said
if you do try to override you should not get a compiler error because of it
I meant what is explained in http://www.coderanch.com/how-to/java/OverridingVsHiding , namely: method hiding, but i was not aware of the terminology(sorry about that) and I was associating what was happening with re-declaring the method in the subclass. Also I was posting the reply with one foot out the door, since I was in a big hurry and by the time i managed to write the reply and submit it Maneesh had already posted a proper explanation( i was still in the writing stages of the reply by the looks of the time posted).
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you.
The link of code ranch posted by "Maneesh Godbole" explains the concept which i already knew but the last paragraph was the part, i was really interested in...
That cleared everything.

Thanks again and regards,
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Vlad wrote:. . .

his is the link i meant: http://geekexplains.blogspot.com/2008/06/can-you-override-static-methods-in-java.html. . . .
Thank you. Although that link correctly shows that static methods behave differently from instance methods, it is confusing because it still uses the word "override" and similar.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Thank you. Although that link correctly shows that static methods behave differently from instance methods, it is confusing because it still uses the word "override" and similar.


Indeed, the coderanch explanation linked by Maneesh is a lot clearer.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course a CodeRanch explanation is better
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link on Overriding vs hiding. It explains these 2 concepts more clearly than online oracle doc that i was reading. By the way, how did you navigate to that link ? I looked under frontpage, and the FAQ does not have anything like this. It would be useful for me to read other javaranch articles, written like this one, to explain java concepts.

Never mind the above question. I found the article under Java Beginner's FAQ
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This brings up another question to my mind, just to clarify things. Is hiding a term, used only when static methods are in play ? It seems so, but i want to make sure.
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While going through the Beginner's FAQ page, i found an error in one of the articles there. It should be corrected by someone with permissions on the page. The following is from the page Interfaces vs Abstract classes

"use an abstract class, if you want to declare non-public members."

Characterization as "non-public" should be changed..... As far as i see in the online oracle doc at Defining an Interface, interfaces can only contain constant (static final) members. Variable fields are only allowed in abstract classes; i quote from oracle online doc....
"However, with abstract classes, you can declare fields that are not static and final,". So the page should reflect the full difference, when it comes to interface/abstract class fields.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:This brings up another question to my mind, just to clarify things. Is hiding a term, used only when static methods are in play ? It seems so, but i want to make sure.


If memory serves, the Java Language Specification uses the term "hiding", in a few places. Hiding in regards to static methods is only one of them. It also discuss "hiding" in relation to static variables and instance variables.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!