Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overide Question

 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • 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:
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • 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
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Jose,
Thanks for the information!
-Matt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic