• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question in MindQ

 
Yi Dong
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Yi Dong
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since ArithmeticException is Runtime exception, it is not the checked exception. so d) is not declaring new checked exception.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think d and e are mutually exclusive. But the most sensible choice between the two is e.
 
Vinay Gangoli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic