Java Reference
In-Depth Information
EXAMPLE: An Enhanced StringTokenizer Class
Inheritance allows you to reuse all the code written for a base class in a derived class and to
reuse it without copying it or even seeing the code in the base class. This means that, among
other things, if one of the standard Java library classes does not have all the methods you
want it to have, you can, in most cases, define a derived class that has the desired additional
methods. In this subsection, we give a simple example of this process. This example requires
that you have already covered the basics about arrays given in Section 6.1 of Chapter 6 and
that you have read the starred section on the StringTokenizer class in Chapter 4. If you
have not covered this material, you will have to skip this example until you cover it.
The StringTokenizer class allows you to generate all the tokens in a string one time, but
sometimes you want to cycle through the tokens a second or third time. There are lots of ways to
accomplish this. For example, you can use the StringTokenizer constructor two (or more) times
to create two (or more) StringTokenizer objects. However, it would be cleaner and more effi-
cient if you could do it with just one StringTokenizer object. Display 7.7 shows a derived class of
the StringTokenizer class that allows you to cycle through the tokens in a string any number of
times. This class is called EnhancedStringTokenizer . The class EnhancedStringTokenizer behaves
exactly the same as the StringTokenizer class, except that the class EnhancedStringTokenizer has
one additional method named tokensSoFar . The method tokensSoFar has no parameters and
returns an array of strings containing all the tokens that have so far been returned by the methods
named nextToken . After an object of the class EnhancedStringTokenizer has gone through all
the tokens with the methods nextToken , it can invoke the method tokensSoFar to produce an
array containing all the tokens. This array can be used to cycle through the tokens any number of
additional times. A simple example of this is given in the program in Display 7.8.
The class EnhancedStringTokenizer has methods, such as countTokens , which it inherits
unchanged from the class StringTokenizer . The class EnhancedStringTokenizer has two methods,
namely the two methods named nextToken , whose definitions are overridden. From the outside, the
methods named nextToken of the class EnhancedStringTokenizer behaves exactly the same as the
methods named nextToken in the class StringTokenizer . However, each of the two methods
named nextToken of the class EnhancedStringTokenizer also saves the tokens in an array instance
variable, a , so that an array of tokens can be returned by the method tokensSoFar . The method
tokensSoFar is the only completely new method in the derived class EnhancedStringTokenizer .
Notice that the definitions of the methods named nextToken in the class EnhancedString-
Tokenizer each includes an invocation of super.nextToken , which is the version of the corre-
sponding 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 vari-
able count contains a count of the number of tokens stored in the array instance variable a . 1
1 The class StringTokenizer also has a method named nextElement with a return type of Object . This method
should also be overridden, but 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 dis-
cuss this point in Self-Test Exercise 23 later in this chapter after we introduce the class Object .
Search WWH ::




Custom Search