I know that overloading uses static binding and overriding uses dynamic binding. But what if they are mixed? According to this tutorial, static binding uses type information to resolve method calls, while dynamic binding uses actual object information.
So, is static binding performed in the following example to determine which sort() method to call?
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: Exit: Child#sort(Collection c) is invoked
source share