Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2008

Tip: Looking for answers? Try searching our database.

Throwing Close To the Edge: a micro quiz for newbies.

Thread view: 
Roedy Green - 27 Jun 2008 07:06 GMT
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.

Class2 extends Class1.  Class2.method overrides Class1.method.

1. is it ok for Class1.method to have some throws that Class2.method
does not:?

2. is it ok for Class2.method to have some throws that Class1.method
does not?

3. is it ok for Class1.method to have throw IOException while
Class2.method throws EOFException (a subclass of IOException)?

4. is it ok for Class2.method to have throw IOException while
Class1.method throws EOFException?

Class2 Implements Interface1.  Class2.method implements
Interface1.method

5. is it ok for Interface1.method to have some throws that
Class2.method does not:?

6. is it ok for Class2.method to have some throws that
Interface1.method does not?

7. is it ok for Interface1.method to have throw IOException while
Class2.method throws EOFException (a subclass of IOException)?

8. is it ok for Class2.method to have throw IOException while
Interface1.method throws EOFException?

Interface2 extends Interface1.

9. is it ok for Interface2 to have an method named the same as in
Interface1?

Signature

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

Hendrik Maryns - 27 Jun 2008 12:56 GMT
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
Roedy Green - 27 Jun 2008 15:17 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.