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 Overide Question 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 "Overide Question" Watch "Overide Question" New topic
Author

Overide Question

Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
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:


-Matt<br /> SCJP2<br /> SCJD
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Thank's as always Valentin.
When I pass my test next week I owe you a cold one.
-Matt
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Jose,
Thanks for the information!
-Matt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overide Question