Dynamic Dispatch

Definitions:

  • dynamic means at runtime
  • static means at compile time.

Is-A Relationship

Suppose we have the following inheritance hierarchy: Animal <- Mammal <- Human.
Let's say we have the following code:

 Human bob = new Human() ;
 Mammal bob2 = bob ;
 Animal bob3 = bob ;

Now, we could say that bob is a Human, and also we could say that bob is a Mammal and bob is an Animal.

Declared vs. Actual Type

In the above code the declared type of bob is Human. The declared type of bob2 is Mammal. The declared type of bob3 is Animal. The declared type is static. That is, it's information we know at compile-time.
However, the actual type of bob, bob2 and bob3 is Human. The actual type is dynamic.

Choice of methods

Suppose now that Animal defines method a1() and a2(), Mammal defines method m1() and m2(), and Human defines method h1() and h2(). You can only call methods a1(), a2(), m1(), and m2() on bob2 since the declared type of bob2 is Mammal.

Bibliography
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.