I am confused on what exactly dispatching is. Especially when it comes to double dispatching. Is there a simple way that I can grasp this concept?
Asked
Active
Viewed 3.2k times
28
-
haha yea... i figured if i understood dispatching, i would get a better understanding of visitor pattern – thunderousNinja Apr 01 '11 at 01:50
-
Ah yes. Sorry, I'll remove the tag. This has nothing to do with design patterns :) – BalusC Apr 01 '11 at 01:51
-
1Just to note that the term "dispatching" is somewhat overloaded. It can also mean the operation of putting scheduled tasks onto a processor. In some real-time schedulers, the concept of a scheduler and a dispatcher are distinct ideas and software components. – andersoj Apr 01 '11 at 02:29
1 Answers
50
Dispatch is the way a language links calls to function/method definitions.
In java, a class may have multiple methods with the same name but different parameter types, and the language specifies that method calls are dispatched to the method with the right number of parameters that has the most specific types that the actual parameters could match. That's static dispatch.
For example,
void foo(String s) { ... }
void foo(Object o) { ... }
{ foo(""); } // statically dispatched to foo(String)
{ foo(new Object()); } // statically dispatched to foo(Object)
{ foo((Object) ""); } // statically dispatched to foo(Object)
Java also has virtual method dispatch. A subclass can override a method declared in a superclass. So at run-time, the JVM has to dispatch the method call to the version of the method that is appropriate to the run-time type of this
.
For example,
class Base { void foo() { ... } }
class Derived extends Base { @Override void foo() { ... } }
{ new Derived().foo(); } // Dynamically dispatched to Derived.foo.
{
Base x = new Base();
x.foo(); // Dynamically dispatched to Base.foo.
x = new Derived(); // x's static type is still Base.
x.foo(); // Dynamically dispatched to Derived.foo.
}
Double-dispatch is the combination of static and run-time(also called dynamic) dispatches.

Mike Samuel
- 118,113
- 30
- 216
- 245
-
This is a really nicely laid out answer. I know Java is not a double-dispatch language but would you be able to add an example of how double dispatch WOULD look like in the context of your current examples? I'm guessing it would be something like `x.foo("")` but I think it would be very helpful to see it. – Ogen Oct 11 '16 at 23:20
-
@Ogen, this is about double dispatch in Java. Could you have meant JavaScript? – Mike Samuel Oct 11 '16 at 23:57
-
@MikeSamuel Yeah I know it's about double dispatch in Java. And I know that Java doesn't actually do double dispatch, but it would still help my understanding to see an example of what double dispatch WOULD look like in java in the context of the examples you have given. – Ogen Oct 12 '16 at 00:21
-
1@Ogen, *Groovy* is a JVM language that, IIRC, does [proper multiple dispatch](https://blogs.oracle.com/sundararajan/entry/multimethods_in_groovy), and any attempts to add the same in Java (perhaps based on [invokedynamic](http://stackoverflow.com/questions/11014917/use-invokedynamic-to-implement-multiple-dispatch)) would probably look similar under the hood. I hope that answers your question. – Mike Samuel Oct 12 '16 at 02:17
-