File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes True/False - Only non-static methods can be overridden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "True/False - Only non-static methods can be overridden" Watch "True/False - Only non-static methods can be overridden" New topic
Author

True/False - Only non-static methods can be overridden

Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Is it true to say that only non-static methods are actually the only "things" which can be truly overridden?
static methods, static fields, non-static fields, all of these are just masked it seems to me.
To clarify, I'm talking about when you use a superclass reference to an object. Polymorphism works nicely with methods, because overridden method is called, but for fields and static methods, the reference type itself determines what is called.
Thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Overriding deals ONLY with instance methods. The entire notion of overriding is to alter "behavior" of a subclass. Fields do not dectate behavrior so they are not even considered when it comes to overriding. Static methods, even though they are methods and do dictate behavior, are not involved in overriding. The JVM does not perform late binding when invoking a static method so no overriding can take place. In fact, if you have two static methods with identical signatures in sub and superclasses, the superclass' method is said to be hidden, not overridden.
I hope that helps,
Corey


SCJP Tipline, etc.
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
What would be the correct answer for the sake of the programmers exam?
Is was asked this sort of question in the Sybex virtual trainer. I was annoyed when I was told that a static method cannot be overridden. With no explaination like what you provided. As I had wrote some code that did at least appear to override the original method.
So I appreciate the explaination Corey McGlone gave. But for the sake of the exam can static methods be overridden or not?
Many thanks, Dave.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Corey, an excellent explanation! Thank you very much, that really drove it home for me.
Dave, based on what Corey said, the answer is most definately NO in my mind.
To me, in terms I currently undestand, overridding is closely tied with polymorphism; Being able to have a superclass reference to an instance of it's subclass.

[ June 02, 2003: Message edited by: Brian Joseph ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Dave Johnson:
But for the sake of the exam can static methods be overridden or not?

NO. Absolutely not. Never. No way. Nu-uh. Nope. Overriding implies dynamic binding which never occurs with static methods. Static methods can not be overridden. Ever.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Is it true to say that only non-static methods are actually the only "things" which can be truly overridden?

This is true for instance methods which can be overridden. So, no final methods. And no private methods, which are implicitly final.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi
I agree with all of you. But according to the way the question is asked shouldn't the answer be true. Becasue "Only non-static methods can be overridden" appears to me that indeed only non-static methods can be overriden. I would appreciate the answers in true or false and please avoid yes and no coz thats the reason I am posting this post as i can't figure out what no in this context means.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Originally posted by Anupam Sinha:
Hi
I would appreciate the answers in true or false and please avoid yes and no coz thats the reason I am posting this post as i can't figure out what no in this context means.

Maybe the question is a little ambiguous, because as stated, the answer is true, as long as the non-static methods (instance methods) are not declared final, and there is proper access for the subclass (public/protected/default).
[ June 03, 2003: Message edited by: Brian Joseph ]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Answer is TRUE
It should have been worded a little differently, perhaps reversed as:
Which is true?
Non-static methods may be overridden.
(in which case that is NOT true)
But, as worded, the answer is still true. It is absolutely correct to say that only non-static methods can be overridden. This does not imply that ALL non-static methods can be overridden, as others have pointed out, but the word 'only' means that "nothing else except", so a different way to phrase it is: "No other method types except those which are non-static can be overridden."
If I saw this on the real exam, I would absolutely say TRUE.
cheers,
- Kathy
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Kathy
I guess that either I missed a point or you missed a word.
I guess that what you have said is infact true
Non-static methods may be overridden.
(in which case that is NOT true)

Either it should be
1. Static methods may be overridden.
or
2. Non-static methods may not be overridden.
In which case both will evaluate to false.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: True/False - Only non-static methods can be overridden