Declaring Overloaded Methods

Class MethodOverload (Fig. D.7) includes two overloaded versions of method square—one that calculates the square of an int (and returns an int) and one that calculates the square of a double (and returns a double). Although these methods have the same name and similar parameter lists and bodies, think of them simply as different methods. It may help to think of the method names as “square of int” and “square of double,” respectively.


 1   // Fig. D.7: MethodOverload.java
 2   // Overloaded method declarations.
 3
 4   public class MethodOverload
 5   {
 6      // test overloaded square methods
 7      public static void main( String[] args )
 8      {
 9         System.out.printf( "Square of integer 7 is %d ", square( 7 ) );
10         System.out.printf( "Square of double 7.5 is %f ", square( 7.5 ) );
11      } // end main
12
13      // square method with int argument                                   
14      public static int square( int intValue )                          
15      {                                                                    
16         System.out.printf( " Called square with int argument: %d ",
17            intValue );                                                    
18         return intValue * intValue;                                      
19      } // end method square with int argument                             
20
21      // square method with double argument                                   
22      public static double square( double doubleValue )                    
23      {                                                                       
24         System.out.printf( " Called square with double argument: %f ",
25            doubleValue );                                                    
26         return doubleValue * doubleValue;                                   
27      } // end method square with double argument                             
28   } // end class MethodOverload

Called square with int argument: 7
Square of integer 7 is 49

Called square with double argument: 7.500000
Square of double 7.5 is 56.250000


Fig. D.7 | Overloaded method declarations.

Line 9 invokes method square with the argument 7. Literal integer values are treated as type int, so the method call in line 9 invokes the version of square at lines 14–19 that specifies an int parameter. Similarly, line 10 invokes method square with the argument 7.5. Literal floating-point values are treated as type double, so the method call in line 10 invokes the version of square at lines 22–27 that specifies a double parameter. Each method first outputs a line of text to prove that the proper method was called in each case. The values in lines 10 and 24 are displayed with the format specifier %f. We did not specify a precision in either case. By default, floating-point values are displayed with six digits of precision if the precision is not specified in the format specifier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset