Page 298 - Introduction to Programming with Java: A Problem Solving Approach
P. 298

                264 Chapter 7 Object-Oriented Programming—Additional Details
 /***********************************************************
*
Height.java
Dean & Dean
This class stores and prints height values.
***********************************************************/
*
*
*
class Height
{
}
// end class Height
double height; // a person's height
String units;
// like cm for centimeters
//********************************************************
public void setHeight(double height)
{
}
}
this.height = height;
this.units = "cm";
//********************************************************
public void setHeight(double height, String units)
{
        Apago PDF Enhancer
this.height = height;
this.units = units;
//********************************************************
public void print()
{
}
System.out.println(this.height + " " + this.units);
Figure 7.10 Height class with overloaded methods
We point out this alternative syntax not because we want you to use it, but because we want you to get a
clearer picture of calling object details.
Program Evolution
The ability to overload a method name promotes graceful program evolution because it corresponds to how natural language regularly overloads the meanings of words. For example, the first version of your program might define just the one-parameter version of its setHeight method. Later, when you decide to enhance your program, it’s easier for your existing users if you minimize the new things they have to learn. In this case, you let them either keep using the original method or switch to the improved method.



























































   296   297   298   299   300