This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can static method be overriden by a static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Can static method be overriden by a static method?" Watch "Can static method be overriden by a static method?" New topic
Author

Can static method be overriden by a static method?

Kathy Wang
Greenhorn

Joined: Sep 06, 2001
Posts: 28
Hi,
I took two related questions from different website,and I foud they seem a little conflicted, can anyone clear me out? Thanks alot!
Q40 from RHE final exam:
Which of the following is/are true?
A.A static method maybe overriden by a static method.
B.A static method maybe overriden by a non-static method.
C.A non-static method maybe overriden by a static method.
D.A non-static method maybe overriden by a non-static method.
Ans: D.
Explaination from book is: A and B are false because static method may noe be overriden.
Question 31) from Marcus' mock exam 2
1) What will happen when you attempt to compile and run the following code
import java.io.*;
class Base{
public static void amethod()throws FileNotFoundException{}
}
public class ExcepDemo extends Base{
public static void main(String argv[]){
ExcepDemo e = new ExcepDemo();
}
public static void amethod(){}
protected ExcepDemo(){
try{
DataInputStream din = new DataInputStream(System.in);
System.out.println("Pausing");
din.readChar();
System.out.println("Continuing");
this.amethod();
}catch(IOException ioe) {}
}
}
1)Compile time error caused by protected constructor
2) Compile time error caused by amethod not declaring Exception
3) Runtime error caused by amethod not declaring Exception
4) Compile and run with output of "Pausing" and "Continuing" after a key is hit
Ans: 4).
Here the static method amethod() has been overriden! I am confused here.
Thanks for your reply!
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Kathy,
The RHE explanatin is correct. A 'static' method cannot be overridden. It can, however, be hidden.
Marcus's code is an example of a static method in the superclass being hidden by a static method of the same name in the subclass.
For a more complete explanation on overriding vs hiding see this Sun Tech Tip
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited September 22, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Kathy Wang
Greenhorn

Joined: Sep 06, 2001
Posts: 28
Thanks Jane, your info does help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can static method be overriden by a static method?
 
Similar Threads
Should shadowed static method follow overriden method rules ?
a question about extends
SCJP Mock Exam Doubts
FileNotFoundException
throw and throws