Which of the following are legal method declarations to add to the class Sub? Assume that each method is the only one being added. a) public static void main( String args[] ){} b) float methodTwo(){} c) long methodOne( int c, long d ){} d) int methodOne( int c, long d ) throws ArithmeticException{} e) int methodOne( int c, long d ) throws FileNotFoundException{} I marked correct answers as a) b) d) e) but d) is not given as correct answer. I my opinion there is no restricition on AritmaticException() when overridding in sub classess. Am I missing something ALkesh
You're right. There have been discussions about it at this forum, one message was posted by Tony,called "Good(IMO) Practice Question" - search for it .(I don't know how to include links in replies).
The answers given is wrong. There is no restriction for an overriding method in throwing a RuntimeException/Error or any subclass of RuntimeException/Error . The compiler doesn't care about it as long as the other overriding rules are followed . And your answers are all correct. regds maha anna
Nilesh Parikh
Ranch Hand
Joined: Mar 14, 2000
Posts: 52
posted
0
hi Maha anna, So can you please put the information regarding this question in the Mock Exam Errata Forum because I found this question in MindQ mack exam ALkesh
I'll move it now. Also, note that actually A is the only one really correct because all the others are declared to return int, long, or float - but none have return statements. So it's a pretty crappy question all around.
It goes like this ... The overriding method must not throw checked exceptions of classes that are not possible for the original method (overridden). There is however no restrictions on numbers of Exceptions.
Originally posted by Chetan Valia: It goes like this ... The overriding method must not throw checked exceptions of classes that are not possible for the original method (overridden). There is however no restrictions on numbers of Exceptions.
Right, and ArithmeticException is not a checked exception, so that code (if it returned the proper data type, as mentioned above...) will compile correctly.
Option D is wrong, and should not be chosen. Because option D is an overriding method, therefore have to follow this rule: "It (Java) will not allow the overriding subclass to be declared as throwing a more general exception or EXCEPTION FROM ANOTHER HIERARCHY." (from <<Java 2 Exam Cram>>, Bill Brogden, page 131) The exceptions (in the super class and in the subclass) are in different branch of the Exception hierarchy. Cheers, Robin