• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and overwriting

 
meng zhou
Greenhorn
Posts: 12
  • 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 choose a) and b), but the anwser is incorrect, why?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you want to be specific then from my point of view non of these methods should compile because they are all missing return statements, on the other hand the question says "methods declaration" but then it provides a body for the method with no return statement!!!
Skipping what i said before and taking the question as it is, then the correct answer would be a,b,d,e.
Overriding method cannot be declared to throw checked exceptions other than those that were declared by the method in the super class.
for example:
void method() throws IOException{} //in your super class
Overriding method can be one of these:
  • throws the same exception : void method() throws IOException{}
  • doesn't throw any exception at all : void method (){}
  • throws a subclass of the exception or an unchecked exception : void method() throws EOFException, ArithmeticException{}

  •  
    Richard Yi
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The ans is exactly abde, but there are some error in Vicken's explanation I think.
    ArithmeticException is extend from RuntimeException, which can be thrown automatically by JVM. This is the exact reason for it is the right ans.
    However, if declaring a method (overridding) throw an Exception not belong to RuntimeException or IOException in this question, it will be illegle!DataFormatException for Example!
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Where do you think my explanation was wrong exactly?!!!
     
    Richard Yi
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh! I'm wrong! I looked the word "unchecked" as "check" in your last point!
    I read it too rapidly!!
     
    meng zhou
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your guys.
    I should have reviewed the Overriding rules once more. as the the overriding methods can throws fewer or narrower checked exceptions, or any unchecked exception.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic