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.
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!
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).]