• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

overriding?  RSS feed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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'
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • 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.


 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!