Java Reference
In-Depth Information
Using Final Methods
Use final methods carefully, because they can inhibit extensibility
of your classes. If the method should not be used by a subclass,
consider making it private. Make it final only if the method must
be accessible by the subclass, but not overridden.
The following steps enter code to create the PasswordInvalidException class
and declare two final methods.
To Create Final Methods
1. Create and then save a new document in the Chapter10 folder, or a
location specified by your instructor, typing PasswordInvalidException
as the file name and selecting Java (*.java) as the file type. Enter lines 1
through 30 as shown in Figure 10-12. In the comments, insert your own
name as programmer and enter the current date.
TextPad displays code for the initial comments, PasswordInvalidException
class header, constructors, and the usage() method in the coding window
(Figure 10-13). The PasswordInvalidException class inherits from
PasswordException by using the keyword, extends. The method, usage(),
overrides the abstract method declared in the parent class. The class
variable, count, is incremented in the constructors.
new Java file
displayed
class variable
count
incremented
in constructors
implements inherited
abstract method
String message returned by
PasswordInvalidException
usage() method
FIGURE 10-13
2. Enter lines 31 through 40 as shown in Figure 10-12.
(continued)
 
Search WWH ::




Custom Search