Java Reference
In-Depth Information
15 . Assume that the ICharQ interface shown in Try This 8-1 has been in widespread use
for several years. Now, you want to add a method to it called reset( ) , which will be
used to reset the queue to its empty, starting condition. Assuming JDK 8 or later, how
can this be accomplished without breaking preexisting code?
To avoid breaking preexisting code, you must use a default interface method. Be-
cause you can't know how to reset each queue implementation, the default reset( )
implementation will need to report an error that indicates that it is not implemented.
(The best way to do this is to use an exception. Exceptions are examined in the fol-
lowing chapter.) Fortunately, since no preexisting code assumes that ICharQ defines
a reset( ) method, no preexisting code will encounter that error, and no preexisting
code will be broken.
16 . How is a static method in an interface called?
A static interface method is called through its interface name, by use of the dot oper-
ator.
Chapter 9: Exception Handling
1 . What class is at the top of the exception hierarchy?
Throwable is at the top of the exception hierarchy.
2 . Briefly explain how to use try and catch .
The try and catch statements work together. Program statements that you want to
monitor for exceptions are contained within a try block. An exception is caught us-
ing catch .
3 . What is wrong with this fragment?
There is no try block preceding the catch statement.
4 . What happens if an exception is not caught?
If an exception is not caught, abnormal program termination results.
5 . What is wrong with this fragment?
Search WWH ::




Custom Search