Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding and overwriting

 
Greenhorn
Posts: 12
  • 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 choose a) and b), but the anwser is incorrect, why?
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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{}

  •  
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Where does a nanny get ground to air missles? Protect this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic