
Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green schreef:
| Here are some questions for newbies on the throws clause you apply to
| a method. The quiz consists of 9 true-false questions. This is
| open-book. You can read the JLS or perform compiler experiments.
| Post your answers if you are feeling brave.
Off the top of my head:
| Class2 extends Class1. Class2.method overrides Class1.method.
|
| 1. is it ok for Class1.method to have some throws that Class2.method
| does not:?
Yes, overriding signatures have to follow Liskov, which in this case
means you can leave out Exceptions (e.g. FileWriter does so).
| 2. is it ok for Class2.method to have some throws that Class1.method
| does not?
Nope, for the same reason (might be that they make exceptions for
RuntimeException)
| 3. is it ok for Class1.method to have throw IOException while
| Class2.method throws EOFException (a subclass of IOException)?
Yes, for the same reason: the overriding method can be more explicit in
the Exceptions it throws.
| 4. is it ok for Class2.method to have throw IOException while
| Class1.method throws EOFException?
Nope. All Liskov.
| Class2 Implements Interface1. Class2.method implements
| Interface1.method
|
| 5. is it ok for Interface1.method to have some throws that
| Class2.method does not:?
Yes. Liskov.
| 6. is it ok for Class2.method to have some throws that
| Interface1.method does not?
No. Liskov.
| 7. is it ok for Interface1.method to have throw IOException while
| Class2.method throws EOFException (a subclass of IOException)?
Yes. Liskov.
| 8. is it ok for Class2.method to have throw IOException while
| Interface1.method throws EOFException?
No. Liskov.
| Interface2 extends Interface1.
|
| 9. is it ok for Interface2 to have an method named the same as in
| Interface1?
What do you mean ‘named’? Same signature? Then I’d guess no,
otherwise: yes, overloading is allowed in Java.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
On Fri, 27 Jun 2008 06:06:57 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :
>3. is it ok for Class1.method to have throw IOException while
>Class2.method throws EOFException (a subclass of IOException)?
To clarify the question. I mean that Class1.method has a throws
declaration for IOException while Class2.method has a declaration for
throws EOFException.
Obviously a method may throw an EOFException of it has declared a
throws IOException.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com