Single Dispatch vs Multiple Dispatch
Single dispatch vs multiple dispatch explained — runtime resolution, Java overload rules and the visitor pattern, with code examples.
Expected Interview Answer
Single dispatch selects which method implementation to run based on the runtime type of only the receiver object the method is called on, while multiple dispatch selects the implementation based on the runtime types of the receiver and one or more additional arguments together.
Java, C#, and most mainstream OOP languages use single dispatch: when you call shape.intersects(other), the method chosen depends purely on shape’s actual runtime class via virtual method dispatch, while other is matched to an overload using its declared (compile-time) type, not its runtime type. This means overloading in Java looks like it considers multiple types, but it is resolved statically at compile time, which is fundamentally different from true multiple dispatch. Multiple dispatch, as found in Julia or Common Lisp’s CLOS, evaluates the runtime types of every relevant argument at the call site and picks the most specific applicable method across all of them, so passing two different runtime types can invoke a genuinely different implementation than passing the reverse pairing. Because single-dispatch languages cannot do this natively, developers approximate it using the visitor pattern (double dispatch through two single-dispatch calls) or explicit instanceof checks, both of which are more verbose than native multiple dispatch but keep the mainstream type system simple and predictable.
- Single dispatch: simple, predictable, and cheap to implement (a vtable lookup)
- Multiple dispatch: naturally expresses symmetric binary operations
- Single dispatch: keeps overload resolution static and compiler-checkable
- Multiple dispatch: avoids sprawling instanceof or visitor boilerplate for type-pair logic
AI Mentor Explanation
A single-dispatch selector board only looks up which drill to run based on who the player is, ignoring what equipment they’re using — a batter always gets the batting drill regardless of whether they’re holding a bat or a helmet-fitting kit. A multiple-dispatch board instead looks up the drill based on both the player’s role and the equipment together, so the same player gets a different drill depending on what they’re holding. That distinction, one factor versus two, is exactly single dispatch versus multiple dispatch.
Step-by-Step Explanation
Step 1
Recognize single dispatch
Java’s virtual method calls pick the implementation from the runtime type of the receiver only; other parameters use static (compile-time) overload resolution.
Step 2
Recognize multiple dispatch
Languages like Julia inspect the runtime types of all relevant arguments together and select the most specific method across the whole set.
Step 3
Spot the gap in single-dispatch languages
A call like a.interact(b) in Java cannot pick behavior based on b’s runtime type without extra machinery.
Step 4
Bridge the gap when needed
Use the visitor pattern (two single-dispatch calls chained to approximate double dispatch) or instanceof chains to simulate multiple dispatch in Java.
What Interviewer Expects
- A precise definition of both terms, not just “dispatch is picking a method”
- Explicit statement that Java overload resolution is compile-time, not runtime, for non-receiver arguments
- Mention of the visitor pattern as the standard Java workaround
- A concrete example illustrating the difference in behavior between the two
Common Mistakes
- Claiming Java method overloading is a form of multiple dispatch (it is compile-time, not runtime, for parameters)
- Believing multiple dispatch is strictly “better” without acknowledging its complexity trade-offs
- Not knowing which mainstream languages use single vs multiple dispatch
- Confusing double dispatch (the visitor-pattern workaround) with true native multiple dispatch
Best Answer (HR Friendly)
“Single dispatch, which Java uses, picks a method based only on the runtime type of the object the method is called on. Multiple dispatch, used in languages like Julia, picks the method based on the runtime types of all the relevant arguments together. Java developers simulate multiple dispatch using the visitor pattern when they need behavior to depend on two objects’ actual types.”
Code Example
// Single dispatch: overload chosen by declared (compile-time) type of "other"
class Shape {
void intersects(Shape other) { System.out.println("generic intersect"); }
void intersects(Circle other) { System.out.println("circle-specific intersect"); }
}
class Circle extends Shape {}
Shape a = new Shape();
Shape b = new Circle(); // declared type is Shape, not Circle
a.intersects(b); // calls intersects(Shape) -- runtime type of b is ignored
// Simulated multiple dispatch via visitor pattern (double dispatch)
interface Visitor { void visitCircle(Circle c); void visitSquare(Square s); }
interface Node { void accept(Visitor v); }
class Square implements Node { public void accept(Visitor v) { v.visitSquare(this); } }
// accept() dispatches on Node’s runtime type, then visitX dispatches on the second typeFollow-up Questions
- Why does Java’s overload resolution not count as true multiple dispatch?
- How does the visitor pattern achieve double dispatch using two single-dispatch calls?
- What performance trade-offs exist between single and multiple dispatch implementations?
- Which mainstream languages, if any, support native multiple dispatch?
MCQ Practice
1. In Java, calling a.method(b), which type determines the overload selected for the parameter b?
Java resolves overloads at compile time using the declared type of arguments, which is the hallmark of single dispatch.
2. True multiple dispatch differs from Java overloading because?
Multiple dispatch is a runtime mechanism that considers the actual types of multiple arguments, unlike Java's compile-time overload resolution.
3. The visitor pattern is best described as?
The visitor pattern chains accept() and visitX() — two single-dispatch virtual calls — to simulate dispatching on two runtime types.
Flash Cards
Single dispatch in one line? — Method chosen by the runtime type of only the receiver object.
Multiple dispatch in one line? — Method chosen by the runtime types of multiple arguments together.
Does Java use single or multiple dispatch? — Single dispatch — overload resolution for parameters is compile-time.
Java’s workaround for multiple dispatch? — The visitor pattern (double dispatch via two chained single-dispatch calls).