• 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?

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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{}

what are the correct answere with special briefing on option
'd' and 'e'
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a. ok
b. ok, this is overloading
c. not ok this is neither overloading (parameter list is exactly the same) nor overriding (return type is not the same)
d. ok this is overriding (return type, method name and parameter list match), moreover the method is declared to throw an ArithmeticException (subclass of RuntimeException) which is perfectly legal for an overriding method
e. ok this is overriding (return type, method name and parameter list match), moreover the method is declared to throw FileNotFoundException (subclass of IOException) which is perfeclty legal for an overriding method
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To further illustrate what's going on here, the following declaration would not compile.
int methodOne( int c, long d ) throws Exception{}
The point is, the overriding method in class Sub is not allowed to throw an Exception that the same method in class Super does not throw.
Method methodOne() in class "Super" explicitly throws IOException. Since FileNotFoundException is a subclass of IOException, it can be thrown by the overriding class "Sub" methodOne().
Method methodOne() in class "Super" implicity throws RuntimeException. All methods do, as these are things that don't need to checked for. Since ArithmeticException is a subclass of RuntimeException, it can be thrown by the overriding class "Sub" methodOne().
Method methodOne() in class "Super" does not throw the generic Exception. Therefore, as in my example, the "Sub" class methodOne() cannot throw Exception.


 
reply
    Bookmark Topic Watch Topic
  • New Topic