Java Reference
In-Depth Information
3. Enter lines 175 through 307 as shown in Figure 10-49 on page 660.
Save and compile the LogonFrame class.
TextPad displays code for the actionPerformed() method in the coding win-
dow (Figure 10-52). The actionPerformed() method is required to implement
the ActionListener interface; it also catches the various password exceptions,
allowing for better error handling. Lines 272 and 283 will exit the program
if more than two invalid password entries are attempted in sequence.
specific password
exceptions caught
specific password
exceptions caught
specific password
exceptions caught
specific password
exceptions caught
most general
exception caught
FIGURE 10-52
Using Adapter Classes
Implementing interfaces is fairly simple, but can become tedious if the
interface declares many methods and you wish to implement only one or two.
To implement the interface in a concrete class, you must define each of the
abstract methods, even if they do not do anything, as shown in the StockFrame
class. An alternative to implementing a number of empty methods of an interface
is to use an adapter class. As you learned in Chapter 5, adapter classes implement
an abstract class, providing prewritten methods for all of the methods of the
abstract class. An adapter class is a concrete class that implements an interface by
providing an empty method for each abstract method declared in the interface.
By creating an object from, or inheriting from, the adapter class, the empty imple-
mentations are inherited. Overriding any method of interest provides the desired
behavior without having to implement each method in your code.
 
Search WWH ::




Custom Search