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
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
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.
Joined: May 16, 2003
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 ]
Joined: Dec 20, 2001
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.
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.
Joined: May 16, 2003
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 ]
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
Joined: Apr 13, 2003
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: True/False - Only non-static methods can be overridden