Page 297 - Introduction to Programming with Java: A Problem Solving Approach
P. 297
But they perform the task on different sets of parameters. Given that situation, overloaded methods are a perfect fit.
Note that the use of overloaded methods is never an absolute requirement. As an alternative, you can always use different method names to distinguish different methods. So why are the above findMaximum method headings better than the below method headings?
int findMaximumOf3Ints(int a, int b, int c)
double findMaximumOf3Doubles(double a, double b, double c)
double findMaximumOf4Doubles(double a, double b, double c, double d)
As these examples suggest, using different method names is cumbersome. With only one method name, the name can be simple. As a programmer, wouldn’t you prefer to use and remember just one simple name rather than several cumbersome names?
An Example
Look at the class in Figure 7.10. It uses overloaded setHeight methods. Both methods assign a height parameter to a height instance variable. The difference is the technique for assigning the height’s units. The first method automatically assigns a hard-coded “cm” (for centimeters) to the units instance variable. The second method assigns a user-specified units parameter to the units instance variable. The second method thus requires two parameters, height and units, whereas the first method requires only one pa- rameter, height. The two methods perform pretty much the same task, with only a slight variation. That’s why we want to use the same name and “overload” that name.
Now look at the driver in Figure 7.11 and its two setHeight method calls. For each method Apago PDF Enhancer
call, can you tell which of the two overloaded methods is called? Figure 7.11’s first method call, setHeight(72.0, "in"),callsFigure7.10’ssecondsetHeightmethodbecausethetwoarguments in the method call match the two parameters in the second method’s heading. Figure 7.11’s second method call, setHeight(180.0), calls Figure 7.10’s first setHeight method because the one argument in the method call matches the one parameter in the first method’s heading.
Calling an Overloaded Method from within an Overloaded Method
Suppose you have overloaded methods and you want one of the overloaded methods to call another one of the overloaded methods. Figure 7.12 provides an example that shows how to do that. Figure 7.12’s setHeight method is an alternative version of Figure 7.10’s one-parameter setHeight method. Note how it calls the two-parameter setHeight method.
The additional method call makes the program slightly less efficient, but some might consider it more elegant because it eliminates code redundancy. In Figure 7.10, this.height = height; appears in both methods, and that’s code redundancy—albeit trivial code redundancy.
Why is there no reference variable dot at the left of the setHeight method call in the body of the method in Figure 7.12? Because if you’re in an instance method, and if you call another method that’s in the same class, the reference variable dot prefix is unnecessary. And in this case, the two overloaded setHeight methods are instance methods and they are indeed in the same class.
WithnoreferencevariabledotprefixinFigure7.12’ssetHeight(height, "cm");methodcall, you might be thinking that the method call has no calling object. Actually, there is an implied calling ob- ject; it’s the same calling object that called the current method. Review quiz: How can you access the cur- rent method’s calling object? Use the this reference. If you want to make the this reference explicit, you can add it to Figure 7.12’s setHeight method call as follows:
this.setHeight(height, "cm");
7.7 Overloaded Methods 263