aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding and overwriting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding and overwriting" Watch "Overriding and overwriting" New topic
Author

Overriding and overwriting

meng zhou
Greenhorn

Joined: Jan 16, 2004
Posts: 12
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

Joined: Jul 21, 2003
Posts: 522
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{}


  • - Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
    Richard Yi
    Greenhorn

    Joined: Feb 04, 2004
    Posts: 16
    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

    Joined: Jul 21, 2003
    Posts: 522
    Where do you think my explanation was wrong exactly?!!!
    Richard Yi
    Greenhorn

    Joined: Feb 04, 2004
    Posts: 16
    Oh! I'm wrong! I looked the word "unchecked" as "check" in your last point!
    I read it too rapidly!!
    meng zhou
    Greenhorn

    Joined: Jan 16, 2004
    Posts: 12
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Overriding and overwriting