• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

A question in MindQ

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think d and e are mutually exclusive. But the most sensible choice between the two is e.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic