私はJavaでメソッドのオーバーロードを実行していて、Eclipseで以下のプログラムの出力を試していました。プログラムは.です。
public class OverloadingTest {
public static void main(String args[]){
List abc = new ArrayList();
List bcd = new LinkedList();
ConfusingOverloading co = new ConfusingOverloading();
co.hasDuplicates(abc); //should call to ArryList overloaded method
co.hasDuplicates(bcd); //should call to LinkedList overloaded method
}
}
class ConfusingOverloading{
public boolean hasDuplicates (List collection){
System.out.println("overloaded method with Type List ");
return true;
}
public boolean hasDuplicates (ArrayList collection){
System.out.println("overloaded method with Type ArrayList ");
return true;
}
public boolean hasDuplicates (LinkedList collection){
System.out.println("overloaded method with Type LinkedList ");
return true;
}
}
そして出力は..
Output
overloaded method with Type List
overloaded method with Type List
説明では、メソッドのオーバーロードはコンパイル時にJavaの静的バインディングを使用して解決されると言われているので、メソッドのオーバーライドによって同じことを実現する方法を教えてください。