• 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

Override class IOException

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
R&H book have a following question:
Consider these classes, defined in separate source files:
1. public class Test1 {
2. public float aMethod(float a, float b)
throws IOException {
3. }
4. }
1. public class Test2 extends Test1 {
2.
3. }
Which of the following methods would be legal (individually) at line 2 in class Test2?
A. float aMethod(float a, float b) { }
B. public int aMethod(int a, int b) throws Exception { }
C. public float aMethod(float a, float b) throws Exception { }
D. public float aMethod(float p, float q) { }
The book indicate letter "C" is wrong, but why??? I don't understand the explanation.
Thanx
 
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
Hi Leandro,
C is wrong because the exception thrown by the overriding method is not compatible with the one specified in the signature of the overridden method. In clear, if a method overrides another method and if exceptions are to be thrown from the overriding method then the exception has to be either:
1. of the same class than the exception thrown by overridden method (i.e. IOException)
2. of a subclass of the exception thrown by overridden method (for instance maybe FileNotFoundException)
3. or no exception at all
So here A is false since you can't override a publid accessible method with a default accesible one.
B is false too since the method (aMethod) is not overridden properly (return type must be the same !!)
C is false (see above)
So here the only valid answer is D since the signature is the same and it throws no exception at all (which is correct)
Hope it helps
Val
 
Valentin Crettaz
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
Sorry folks,
B is true, I did not look at it properly...
The method aMethod is overloaded ...
Sorry again
To summarize, B and D are true (A and C false)
Val
 
Leandro Silva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx for your help Val!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic