Overload.java

Download Overload.java

 1: // Demonstration of Java overloaded name resolution.  In Java,
 2: // if the method call ("invocation") doesn't exactly match any
 3: // method signature, then the closest match where the arguments
 4: // are of a larger size (or more general object type) is called.
 5: //
 6: // Written 1999-2006 by Wayne Pollock, Tampa Florida USA.
 7: 
 8: public class Overload
 9: {
10:    public static void main ( String [] args )
11:    {
12:       aMethod( 3 );
13:    }
14: 
15:    static void aMethod ( double d )
16:    {
17:       System.out.println( "aMethod(double) was called" );
18:    }
19: 
20: /*
21: // Methods that only differ in their return types or modifiers
22: // are NOT allowed.  So the following would cause errors:
23: 
24:    static int aMethod ( double d ) {}
25: 
26:    public static void aMethod ( double d ) {}
27: */
28: 
29:    static void aMethod ( long l )  // Comment out this method
30:    {                               // and see what happens!
31:       System.out.println( "aMethod(long) was called" );
32:    }
33: 
34:    static void aMethod ( float f )
35:    {
36:       System.out.println( "aMethod(float) was called" );
37:    }
38: }
39: 
40: 
41: /* Test your understanding:  What happens in the following case?
42: 
43:    testMethod( 3, 4 );
44: 
45: 
46:    static void testMethod ( int i, double d )
47:    {
48:       System.out.println( "testMethod(int,double) was called" );
49:    }
50: 
51:    static void testMethod ( double d, int i )
52:    {
53:       System.out.println( "testMethod(double,int) was called" );
54:    }
55: 
56: */