/* * Objects in Java retain knowledge of their creation * class even when they are cast to another class. As a * result, dynamic dispatch means that the function is * called from bb AA: AA BB: BB bb cast to AA: BB Had java used static method dispatch the output would be AA: AA BB: BB bb cast to AA: AA * @author gtowell *created: Sep 26. 2021 * modified: Sep 2022 gtowell */ public class DMD { // a class private class AA { @Override public String toString() { return "AA!"; } } // another class, inherits from first private class BB extends AA { @Override public String toString() { return "BB!!"; } } public static void main(String[] args) { new DMD().doWork(); } // Just print the argument using its toString public void prA(AA item) { System.out.println(item); } public void doWork() { AA aa = new AA(); BB bb = new BB(); System.out.println("Just calling the toString methods of each inner class"); System.out.println("AA: " + aa); System.out.println("BB: " + bb); System.out.println("aa left as AA is function call"); prA(aa); System.out.println("bb coerced to AA is function call"); prA(bb); } }