Java Reference
In-Depth Information
so, it may inherit the same method signature more than once. This is no problem
because the methods are abstract and must be overridden anyway.
• Flexibility with arrays. Interfaces provide a way of writing one method that
can work with many different types. For example, using interface Comparable
or interface Comparator , we can write one procedure to sort any array whose
elements are of any class C that implements Comparable .
• Writing loops. By properly implementing interface Enumerator or interface
Iterator, we can make it easy to write loops that enumerate sequences of val-
ues. For example, we can make it easy to write a loop to sequence through the
characters of a string or the prime numbers in some range or the tags on an html
• Listeners. The interface is the prime mechanism for connecting an event in a
GUI, like a press of a button or a keystroke, with a method, called a listener , to
process it.
• Nested classes. A nested class is a class that is declared within another class.
The reasons for using a nested class are: (1) to allow the nested class to refer
directly to components of the outer class, (2) to reduce the proliferation of .java
files, (3) to provide for better information hiding, and (4) to provide for better
software engineering
• Kinds of nested class. A static nested class is a static class that is declared as
a component of another class. An inner class is a non-static class that is declared
either as a component of some class or in a method of some class —in the latter
case, the class is called a local inner class .
• Anonymous class. If a local inner class is referenced in only one place, Java
has a syntax for it that eliminates the need to give the class a name. When one
uses this syntax, the class is called an anonymous class.
• Flattened view of inner classes. In our model, the file drawer for an inner class
appears within the file drawer for the class in which it is declared. This model,
along with Java's inside-out rules, helps clarify the concept of an inner class and
the reasons for using them. When Java is executing a program, however, a more
flattened view of the classes is implemented in which no file drawer for a class
resides in the file drawer for another class.
Exercises for Chapter 12
E1. Activity 8-5.3 of the CD ProgramLive contains a footnote that has a link to
a file, , that contains several methods that process arrays of int s.
Obtain that file and modify the methods so that they process arrays of any base
type that implements interface Comparable . Be sure to test your methods thor-
Search WWH ::

Custom Search