Java Reference
In-Depth Information
EXAMPLE:
(continued)
Notice that the definitions of the methods named
nextToken
in the class
EnhancedStringTokenizer
each include an invocation of
super.nextToken
,
which is the version of the corresponding method
nextToken
in the base class
StringTokenizer
. Each overridden version of the method
nextToken
uses the
method
super.nextToken
to produce the token it returns, but before returning the
token, it stores the token in the array instance variable
a
. The instance variable
count
contains a count of the number of tokens stored in the array instance variable
a
.
1
Display 7.7
Enhanced
StringTokenizer
(part 1 of 2)
1
import
java.util.StringTokenizer;
2
3
public class
EnhancedStringTokenizer
extends
StringTokenizer
4 {
5
private
String[] a;
6
private int
count;
7
public
EnhancedStringTokenizer(String theString)
8 {
9
The method
countTokens
is inherited
and is not overridden.
super
(theString);
10 a =
new
String[countTokens()];
11 count = 0;
12 }
13
public
EnhancedStringTokenizer(String theString, String delimiters)
14 {
15
super
(theString, delimiters);
16 a =
new
String[countTokens()];
17 count = 0;
18 }
19
/**
20
Returns the same value as the same method in the StringTokenizer
21
class, but it also stores data for the method tokensSoFar to use.
22
*/
This method
nextToken
has its definition
overridden.
23
public
String nextToken()
24 {
1
The class
StringTokenizer
also has a method named
nextElement
with a return type
of
Object
. This method should also be overridden. We have not yet even mentioned this
method because we have not yet discussed the class
Object
. For now, you can simply pretend
StringTokenizer
has no such method
nextElement
. We will discuss this point in Self-
Test Exercise 23 later in this chapter after we introduce the class
Object
.