• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic