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.
A
static
interface method is called through its interface name, by use of the dot oper-
ator.
Chapter 9: Exception Handling
Throwable
is at the top of the exception hierarchy.
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
.
There is no
try
block preceding the
catch
statement.
If an exception is not caught, abnormal program termination results.