Java Reference
In-Depth Information
Display 4.12
Using an Overloaded Method Name (part 2 of 2)
8
date1.setDate(1, 2, 2008);
9
date2.setDate("February", 2, 2008);
10
date3.setDate(2008);
11
System.out.println(date1);
12
System.out.println(date2);
13
System.out.println(date3);
14
}
15 }
Sample Dialogue
January 2, 2008
February 2, 2008
January 1, 2008
Overloading and Automatic Type Conversion
Java always looks for a method signature that exactly matches the method invocation
before it tries to use automatic type conversion. If Java can find a definition of a method that
exactly matches the types of the arguments, it uses that definition. Only after it fails to find
an exact match does Java try automatic type conversions to find a method definition that
matches the (type cast) types of the method invocation.
PITFALL: You Cannot Overload Based on the Type Returned
Note that the signature of a method lists only the method name and the types of the
parameters and does not include the type returned. When you overload a method
name, any two methods must have different signatures. The type returned has
nothing to do with the signature of a method. For example, a class could not have
two method definitions with the following headings:
public class SampleClass2
{
public int computeSomething( int n)
.
.
.
public double computeSomething( int n)
.
.
.
 
 
Search WWH ::




Custom Search