System, but they may not be reproduced for publication


particular circumstance. You, the programmer, need only remember the general



Yüklə 83 Mb.
Pdf görüntüsü
səhifə32/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   28   29   30   31   32   33   34   35   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )


particular circumstance. You, the programmer, need only remember the general
operation being performed. Through the application of polymorphism, several names
have been reduced to one. Although this example is fairly simple, if you expand the
concept, you can see how overloading can help manage greater complexity.
When you overload a method, each version of that method can perform any activity you
desire. There is no rule stating that overloaded methods must relate to one another.
However, from a stylistic point of view, method overloading implies a relationship.
Thus, while you can use the same name to overload unrelated methods, you should not.
For example, you could use the name sqr to create methods that return the square of
an integer and the square root of a floating­point value. But these two operations are
fundamentally different. Applying method overloading in this manner defeats its
original purpose. In practice, you should overload only closely related operations.


Ask the Expert
Q
: I’ve heard the term signature used by Java programmers. What is
it?
A
: As it applies to Java, a signature is the name of a method plus its parameter
list. Thus, for the purposes of overloading, no two methods within the same class
can have the same signature. Notice that a signature does not include the return
type, since it is not used by Java for overload resolution.
OVERLOADING CONSTRUCTORS
Like methods, constructors can also be overloaded. Doing so allows you to construct
objects in a variety of ways. For example, consider the following program:


The output from the program is shown here:
MyClass( ) is overloaded four ways, each constructing an object differently. The
proper constructor is called based upon the parameters specified when new is
executed. By overloading a class’ constructor, you give the user of your class flexibility


in the way objects are constructed.
One of the most common reasons that constructors are overloaded is to allow one
object to initialize another. For example, consider this program that uses the
Summation class to compute the summation of an integer value:
The output is shown here:
Often, as this example shows, an advantage of providing a constructor that uses one
object to initialize another is efficiency. In this case, when s2 is constructed, it is not
necessary to recompute the summation. Of course, even in cases when efficiency is not
an issue, it is often useful to provide a constructor that makes a copy of an object.
Try This 6­2
Overloading the Queue Constructor


In this project, you will enhance the Queue class by giving it two additional
constructors. The first will construct a new queue from another queue. The second will
construct a queue, giving it initial values. As you will see, adding these constructors
enhances the usability of Queue substantially.
1. Create a file called QDemo2.java and copy the updated Queue class from 
Try This
6­1
 into it.
2. First, add the following constructor, which constructs a queue from a queue.
Look closely at this constructor. It initializes putloc and getloc to the values
contained in the ob parameter. It then allocates a new array to hold the queue and
copies the elements from ob into that array. Once constructed, the new queue will be
an identical copy of the original, but both will be completely separate objects.
3. Now add the constructor that initializes the queue from a character array, as shown
here:
This constructor creates a queue large enough to hold the characters in a and then
stores those characters in the queue.


4. Here is the complete updated Queue class along with the QDemo2 class, which
demonstrates it:




The output from the program is shown here:
RECURSION
In Java, a method can call itself. This process is called recursion, and a method that
calls itself is said to be recursive. In general, recursion is the process of defining
something in terms of itself and is somewhat similar to a circular definition. The key
component of a recursive method is a statement that executes a call to itself. Recursion
is a powerful control mechanism.
The classic example of recursion is the computation of the factorial of a number. The

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   28   29   30   31   32   33   34   35   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə