オーバーロードは静的バインディングを使用し、オーバーライドは動的バインディングを使用することを知っています。しかし、それらが混在している場合はどうなりますか?このチュートリアルによると、メソッド呼び出しを解決するために、静的バインディングは型情報を使用し、動的バインディングは実際のオブジェクト情報を使用します。
sort()
では、次の例では、どのメソッドを呼び出すかを決定するために静的バインディングが行われているのでしょうか?
public class TestStaticAndDynamicBinding {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
Parent p = new Child();
Collection c = new HashSet();
p.sort(c);
}
}
.
public class Parent {
public void sort(Collection c) {
System.out.println("Parent#sort(Collection c) is invoked");
}
public void sort(HashSet c) {
System.out.println("Parent#sort(HashSet c) is invoked");
}
}
.
public class Child extends Parent {
public void sort(Collection c) {
System.out.println("Child#sort(Collection c) is invoked");
}
public void sort(HashSet c) {
System.out.println("Child#sort(HashSet c) is invoked");
}
}
ps: 出力は次のとおりです。
Child#sort(Collection c) is invoked