aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question in MindQ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question in MindQ" Watch "A question in MindQ" New topic
Author

A question in MindQ

Yi Dong
Greenhorn

Joined: May 19, 2003
Posts: 22
33. Consider the classes defined below:
import java.io.*;
class Super
{
int methodOne( int a, long b ) throws IOException
{ // code that performs some calculations
}
float methodTwo( char a, int b )
{ // code that performs other calculations
}
}
public class Sub extends Super
{
}
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 chose a), b), d), e)
but the answer is a, b, e without d
Anybody know the reason ?
Thanks in advance !
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

the method is a good candidate for overriding (same return type, same method name, and same argument type). BUT, an override method must not throw any new or broader
checked exceptions. AritmeticException is a new exception you're declaring (it is a runtime exception), so it is an illegal override
[ June 26, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Yi Dong
Greenhorn

Joined: May 19, 2003
Posts: 22
Since ArithmeticException is Runtime exception, it is not the checked exception. so d) is not declaring new checked exception.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
My apologies Yi.. since it is a runtime exception and not a checked exception there's no problem with this declaration.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
I think d and e are mutually exclusive. But the most sensible choice between the two is e.
Vinay Gangoli
Greenhorn

Joined: Jun 13, 2003
Posts: 22
Hi all,

From what i understand the overriding method can only throw fewer or more specific exceptions.
In the Exception hierarchy the FileNotFound Exception is a subclass of IOException (hence can be thrown)while the ArithmeticException is not( hence cant be thrown).
Correct me if i am worng.
Vinay


SCJP 1.4<br />" Somedays ur the dog...other days ur the water hydrant "
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Vinay Gangoli:
Hi all,

From what i understand the overriding method can only throw fewer or more specific exceptions.

you forgot to add checked exceptions
"the rules only applies to checked exceptions -- exceptions which are subclass of Exception-- but not subclasses of RuntimeException".
taken from the Java Rule Round Up
[ June 26, 2003: Message edited by: Andres Gonzalez ]
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
Yi, I think is a clear mistake of the auter. d is very valid. In JLS 8.4.4 is clearly stated that the overriding method can not declare to throw more checked exceptions then the overidden method. Beside that, try to compile it, ans you wills e it works!
Miki


Miki<br /> <br />SCJP 1.4, SCBCD 1.3
 
Don't get me started about those stupid light bulbs.
 
subject: A question in MindQ
 
Similar Threads
MindQ question 33 - throwing ArithmeticException
overriding?
methods
doubt in exception
re; MindQ test question