Java Reference
In-Depth Information
Using a polymorphic reference as the formal parameter to a method is a power-
ful technique. It allows the method to control the types of parameters passed into
it, yet gives it the flexibility to accept arguments of various types.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 10.10 How can polymorphism be accomplished using interfaces?
SR 10.11 Suppose that the Speaker interface and the Philosopher and Dog
classes are as described in this section. Are the following sequences of
statements legal? Explain.
a. Speaker current = new Speaker();
b. Speaker current = new Dog();
c. Speaker first, second;
first = new Dog();
second = new Philosopher();
first.speak();
first = second;
d. Speaker first = new Dog();
Philospher second = new Philosopher();
second.pontificate();
first = second;
e. Speaker first = new Dog();
Philospher second = new Philosopher();
first = second;
second.pontificate();
first.pontificate();
10.4 Sorting
Let's examine a problem that lends itself to a polymorphic solution. Sorting is the
process of arranging a list of items in a well-defined order. For example, you may
want to alphabetize a list of names or put a list of survey results into descending
numeric order. Many sorting algorithms have been developed and critiqued over the
years. In fact, sorting is considered to be a classic area of study in computer science.
This section examines two sorting algorithms: selection sort and insertion
sort. Complete coverage of various sorting techniques is beyond the scope of this
 
Search WWH ::




Custom Search