Overloading of Generic methods

The draft Java Language Specification has this to say about Overloading.
8.4.9 Overloading
When a method is invoked, the number of actual arguments (and any
explicit type arguments) and the compile-time types of the arguments are used, at
compile time, to determine the signature of the method that will be invoked.
The difference here is the extra rule within braces.
This is the JLS second edition.
When a method is invoked, the number of actual arguments and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked.
Now code written using JDK 5.0 relies on the type arguments to determine the correct method even if we have two methods with the same name and no parameters like the following code.
public class Overloading {

 public static <A extends String> A test() {
     System.out.println(“String”);
     return null;
   }
  
 public static <B extends Integer> B test() {
     System.out.println(“Number”);
     return null;
 }
 
 public static void main(String[] args) {
  Overloading.<Integer>test();
  Overloading.<String>test();
 }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: