• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can You Override Static Methods??

 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that you can as long as you override with a static method. Now I'm hearing about shadowing is what really happens with static methods. Are we talking semantics here?
I've refered to the JLS, but I would be grateful for a succinct answer Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's semantics mostly, but there are specific differences between hiding of static methods, and overriding of non-static methods. Overridden methods are resolved dynamically, while hidden methods are not. The JLS uses two different terms to describe similar-but-different behavior.
 
Steve Deadsea
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roedy Green wrote a section on this subject in his Java Glossary.
http://mindprod.com/gotchas.html#OVERRIDE
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you Jim and Steve. I really appreciate the straighforward answer, becuse as you know the difference between right and wrong on these Exams is a very, very, very thin line. So If I may trouble you once again, If you were asked if static methods could be overriden you would no, they're actually hidden?
Anyway I think I got the message, thanks.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.
Static methods are resolved at compile time ("early binding") and can be hidden.
Other methods are resolved at run time and only this "late binding" is considered overriding.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiding is what happens when you interfere with inheritance, so a static method prevents a class from inheriting a method by hiding.
Shadowing is what happens when you interfere with scope, so a local variable can shadow the member variable that would have been used if the local variable did not have the same name.
Obscuring is what happens when you interfere with the namespace of another thing. So if you name a variable with the same name as a Class, the variable will obscure the class. The rules specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package.
[ April 11, 2002: Message edited by: Cindy Glass ]
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much appreciated Cindy, thank-you very much. Mike.
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, that's the most plain spoke definition I've seen on Hiding, Shadowing, Obscuring. Again much appreciated. I've been wrestling with this concept for 2 days.
[ April 11, 2002: Message edited by: Mike Kelly ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were asked if static methods could be overriden you would no, they're actually hidden?
Errr... if I was asked this in the programmer certification exam, I would consider that there's a definite possibility that the exam authors are using the term incorrectly. If one of the answer choices allowed me to make a distinction between overriding and hiding, I would choose that one. But otherwise, I'd probably assume they meant "hiding". I know RHE had sloppy wording on this issue (in 1st ed. at least), so I don't believe Simon Roberts expected anyone to make this distinction. This may have been fixed subsequently; I don't know.
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like the exact wording in the RHE book is something like: A static method may not be overriden by a non-static method (although I don't have the book in front of me at the moment). This would seem to indicate that a static method may be over-riden by a static method. Although, as you've seen above, technically that is not true, for purposes of the exam I think that I would not be too technical with this terminology. A static method that is "overriden" by another static method will not cause a compiler error nor a runtime exception.
[ April 11, 2002: Message edited by: Marilyn deQueiroz ]
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marilyn. Jqplus Question
Question ID :970935900390
Which of the following method definitions will prevent overriding of that method?
public final void method m1()
public static void method m1()
public final static void method m1()
public abstract void method m1()
private void method m1()
With the correct answers being 1,2,3.
Option 2 threw me for the past 2 days.
[ April 12, 2002: Message edited by: Mike Kelly ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic