Method overloading in Java

The following code snippet has three versions of the method called show() .

 package overloading; import java.util.ArrayList; import java.util.List; public final class Main { private void show(Object object) { System.out.println("Object"); } private void show(List<Object> list) //Unused method { System.out.println("List"); } private void show(Object[] objects) { System.out.println("Objects"); } private void addToList() { List<String>list=new ArrayList<String>(); list.add("String1"); list.add("String2"); list.add("String3"); show(list); // Invokes the first version String []s={"111", "222", "333"}; show(s); // Invokes the last version } public static void main(String[] args) { new Main().addToList(); } } 

In this simplest of Java code, this method calls show(s); (the last line in the addToList() method) calls the latest version of the overloaded methods. It supplies an array of strings - String[] and is accepted by a receive parameter of type Object[] .

This call to the show(list); function show(list); , however, tries to call the first version of the overloaded methods. It passes a list of strings of type List<String> , which should be accepted by the middle version, whose receive parameter is of type List<Object> . The average version of the methods is not fully used. This is a compile-time error if the first version is removed.

Why is this call show(list); does not call this version - private void show(List<Object> list){} - medium?

+4
source share
3 answers

In short, List<Object> NOT List<String> .

To "fix" your code, use the following code

 private void show(List<? extends Object> list) { System.out.println("List"); } 

Unlike arrays (which are covariant in Java), different instances of a generic type are incompatible with each other, not even explicitly.

With the declaration of Generic<Supertype> superGeneric; Generic<Subtype> subGeneric; Generic<Supertype> superGeneric; Generic<Subtype> subGeneric; the compiler will report a conversion error for both castings (Generic<Subtype>)superGeneric and (Generic<Supertype>)subGeneric .

This incompatibility can be mitigated with a wildcard if ? used as the actual parameter type: Generic<?> - an abstract supertype for all instances of the generic type.

Also see

+7
source

List<Object> not a superclass of List<String> in java. What you are assuming is that Java has a generic covariance, which is NOT.

This means that if A is a superclass of B , List<A> NOT a superclass of List<B>

A similar problem occurs in Cannot convert a generic type to an extended nested type . You can see if any work is working around you.

Subject to change

 private void show(List<Object> list) 

to

 private void show(List<? extends Object> list) 

Will work as you expected?

+4
source

I would say that this is because the parameters are different, List <Object> Different from List <String> Therefore, when you call an overloaded method, it will be the first to accept only the object by default.

Here is a quick example:

 public class Test { public static void overload (Object o) { System.out.println ("Object"); } public static void overload (List <Object> o) { System.out.println ("List Object"); } public static void main (String [] args) { overload (new ArrayList <Object>()); //"List Object" overload (new ArrayList <String>()); //"Object" } } 

Parameterize the list using generics, and everything should work.

+3
source

All Articles