aspose file tools*
The moose likes Java in General and the fly likes Can You Override Static Methods?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can You Override Static Methods??" Watch "Can You Override Static Methods??" New topic
Author

Can You Override Static Methods??

Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
Roedy Green wrote a section on this subject in his Java Glossary.
http://mindprod.com/gotchas.html#OVERRIDE
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Jul 18, 2001
Posts: 78
Much appreciated Cindy, thank-you very much. Mike.
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jul 22, 2000
Posts: 9053
    
  12
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 ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can You Override Static Methods??