Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overloading and generics

Java typically prefers normal methods to generic ones when choosing which overloaded method is correct, which could generate the following sscce:

public class GenericsTest {
    public static void main(String[] args) {
        myMethod(Integer.class, 10);
        myMethod(String.class, "overloaded method");
    }

    public static <T> void myMethod(Class<T> klass, T foo) {
        System.out.println("hello world");
    }

    public static <T> void myMethod(Class<T> klass, String bar) {
        System.out.println(bar);
    }
}

Output:

hello world
overloaded method

Is there any way to force Java to use the Generic version?

like image 631
durron597 Avatar asked Jun 19 '13 13:06

durron597


People also ask

Can generic methods be overloaded?

A generic method can also be overloaded by nongeneric methods. When the compiler encounters a method call, it searches for the method declaration that best matches the method name and the argument types specified in the call—an error occurs if two or more overloaded methods both could be considered best ...

Can we overload a generic method in C#?

Answer: Yes, we can overload a generic methods in C# as we overload a normal method in a class. Q- If we overload generic method in C# with specific data type which one would get called? Answer: Function with specific data type i.e int will be called.

What are generics methods in Java?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

What is generics and its purpose?

In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.


3 Answers

No, not short of deleting or hiding the more specific overload. Yet, if they behave differently, they should simply have different names. And if they behave the same, it should not matter either way.

like image 165
Ben Schulz Avatar answered Oct 19 '22 04:10

Ben Schulz


One approach I've seen is to add a dummy parameter to the less frequently used method:

public static <T> void myMethod(Class<T> klass, String bar, Void ignored) {
    System.out.println(bar);
}

calling it like

myMethod(String.class, "overloaded method", null);

but otherwise

myMethod(String.class, "overloaded method");

calls the generic method.

like image 42
kuporific Avatar answered Oct 19 '22 04:10

kuporific


Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used.

(from here)

like image 32
davek Avatar answered Oct 19 '22 05:10

davek