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)
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?
source share