Java Reference
In-Depth Information
For example, in Fig. 6.10, the compiler might (internally) use the logical name
square of int ” for the square method that specifies an int parameter and “ square of
double ” for the square method that specifies a double parameter (the actual names the
compiler uses are messier). If method1 's declaration begins as
void method1( int a, float b)
then the compiler might use the logical name “ method1 of int and float .” If the param-
eters are specified as
void method1( float a, int b)
then the compiler might use the logical name “ method1 of float and int .” The order of
the parameter types is important—the compiler considers the preceding two method1
headers to be distinct .
Return Types of Overloaded Methods
In discussing the logical names of methods used by the compiler, we did not mention the
return types of the methods. Method calls cannot be distinguished only by return type . If you
had overloaded methods that differed only by their return types and you called one of the
methods in a standalone statement as in:
square( 2 );
the compiler would not be able to determine the version of the method to call, because the
return value is ignored . When two methods have the same signature and different return
types, the compiler issues an error message indicating that the method is already defined in
the class. Overloaded methods can have different return types if the methods have different
parameter lists. Also, overloaded methods need not have the same number of parameters.
Common Programming Error 6.8
Declaring overloaded methods with identical parameter lists is a compilation error re-
gardless of whether the return types are different.
6.13 (Optional) GUI and Graphics Case Study: Colors
and Filled Shapes
Although you can create many interesting designs with just lines and basic shapes, class
Graphics provides many more capabilities. The next two features we introduce are colors
and filled shapes. Adding color enriches the drawings a user sees on the computer screen.
Shapes can be filled with solid colors.
Colors displayed on computer screens are defined by their red , green , and blue com-
ponents (called RGB values ) that have integer values from 0 to 255. The higher the value
of a component color, the richer that color's shade will be. Java uses class Color (package
java.awt ) to represent colors using their RGB values. For convenience, class Color con-
tains various predefined static Color objects— BLACK , BLUE , CYAN , DARK_GRAY , GRAY ,
GREEN , LIGHT_GRAY , MAGENTA , ORANGE , PINK , RED , WHITE and YELLOW . Each can be accessed
via the class name and a dot ( . ) as in Color.RED . You can create custom colors by passing
the red-, green- and blue-component values to class Color 's constructor:
public Color( int r, int g, int b)
 
 
Search WWH ::




Custom Search