• 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

Overide Question

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
This question is from J2 Cert Guide, final exam question 40:
Which of the following is/are true?
A. A static method may be overridden by a static
method.
b. A static method may be overridden by a non-
static method.
c. A non-static method may be overridden by a
static method.
d. A non-static method may be overridden by a
final non-static method.
The book answer is D.
I want to make sure I am understanding why A & B are not true.
Is it because static means that it belongs to the class, therefore if you subclass you do not get that static method unless you use it through a parent class reference. Now if you overide the static method in your code, your not really overriding it, you are merely making a new static method for the child class, because the parent classes stays with the parent.
True?
Thanks,
-Matt
Here is some code, to hopefully proove my theory:
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now if you overide the static method in your code, your not really overriding it, you are merely making a new static method for the child class, because the parent classes stays with the parent.
That's correct Matt. This is called hiding.
The rule of thumb is:
static methods may hide static methods.
non-static methods may override non-static methods.
Period
No mix and match between static and non-static methods.If you want more information, please read JLS 8.4.6 Inheritance, Overriding, and Hiding
 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank's as always Valentin.
When I pass my test next week I owe you a cold one.
-Matt
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I pass my test next week...
When is it again? Well, I wish you good luck in advance
...I owe you a cold one
I heard that
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Matt
Just a minor point:


Is it because static means that it belongs to the class, therefore if you subclass you do not get that static method unless you use it through a parent class reference.


You can access a class method defined in a superclass via a reference to the derived one. Static methods are inherited, though they can not be overriden just hidden.
 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose,
Thanks for the information!
-Matt
 
reply
    Bookmark Topic Watch Topic
  • New Topic