What is Multiple Dispatch?
Learn multiple dispatch — method selection by runtime types of several arguments — vs single dispatch, with a Java visitor-pattern example.
Expected Interview Answer
Multiple dispatch is a form of polymorphism in which the specific method implementation invoked is chosen at runtime based on the runtime types of more than one argument, not just the type of the object the method is called on.
In a single-dispatch language like Java, calling shape.intersects(other) selects the method purely by the runtime type of shape; the parameter’s declared type is used, so if a more specific overload exists for the actual runtime type of other, it is not automatically chosen. Multiple dispatch, found natively in languages like Common Lisp (CLOS) and Julia, instead examines the runtime types of every relevant argument to select the most specific applicable method, so collide(circle, rectangle) and collide(circle, circle) can each resolve to genuinely different implementations based on both operands’ actual types. Languages without native multiple dispatch commonly simulate it with the visitor pattern or manual type-checking chains (instanceof cascades) to get the same effect. The trade-off is that multiple dispatch is more expressive for symmetric binary operations (like collision detection or arithmetic between mixed number types) but is harder to reason about and implement efficiently than single dispatch.
- Naturally expresses binary operations where both operand types matter
- Avoids long instanceof/if-else chains for type-pair-specific behavior
- Keeps each type-pair’s logic in its own dedicated method
- Matches real-world binary relationships (collisions, combinations) more directly
AI Mentor Explanation
How an umpire rules on a caught-behind appeal depends on both who is bowling, a spinner or a pacer, and what kind of delivery was played, a defensive block or an aggressive drive; the same appeal produces different rulings depending on that combination of two factors, not just one. A single-dispatch system would only look at the bowler’s type and ignore the shot played. Multiple dispatch mirrors this: the outcome is chosen by examining the runtime types of two participants together, not just one.
Step-by-Step Explanation
Step 1
Identify the multi-type behavior
Recognize when behavior genuinely depends on the combination of two or more runtime types (e.g. collision between two shape types).
Step 2
Choose a mechanism
Use a language with native multiple dispatch (Julia, CLOS), or simulate it in single-dispatch languages via the visitor pattern or instanceof chains.
Step 3
Define behavior per type pair
Write a dedicated method or visitor case for each meaningful combination of types.
Step 4
Dispatch resolves at runtime
The runtime inspects the actual types of all relevant arguments and selects the most specific matching implementation.
What Interviewer Expects
- A clear distinction from single dispatch (Java’s default method resolution)
- Awareness that Java simulates multiple dispatch via the visitor pattern or instanceof
- An example language with native multiple dispatch (e.g. Julia, Common Lisp/CLOS)
- A concrete scenario where multiple dispatch is genuinely useful (e.g. collision detection)
Common Mistakes
- Confusing multiple dispatch with simple method overloading (overloading is resolved at compile time by declared types)
- Claiming Java supports true multiple dispatch natively (it does not)
- Not knowing the visitor pattern is the standard workaround in single-dispatch languages
- Ignoring the performance and complexity trade-offs of multiple dispatch
Best Answer (HR Friendly)
“Multiple dispatch means the program picks which method to run by looking at the actual types of more than one object involved, not just the object the method was called on. Java doesn’t support this natively, so developers usually simulate it with the visitor pattern, but languages like Julia support it directly.”
Code Example
interface Shape {
void accept(ShapeVisitor visitor);
}
interface ShapeVisitor {
void visitCircle(Circle c);
void visitRectangle(Rectangle r);
}
class Circle implements Shape {
public void accept(ShapeVisitor visitor) { visitor.visitCircle(this); }
}
class Rectangle implements Shape {
public void accept(ShapeVisitor visitor) { visitor.visitRectangle(this); }
}
class CollisionVisitor implements ShapeVisitor {
private final Shape other;
CollisionVisitor(Shape other) { this.other = other; }
public void visitCircle(Circle c) { System.out.println("Collide with circle"); }
public void visitRectangle(Rectangle r) { System.out.println("Collide with rectangle"); }
}
// double dispatch: shape.accept resolves the first type, visitX resolves the second
Shape a = new Circle();
Shape b = new Rectangle();
a.accept(new CollisionVisitor(b));Follow-up Questions
- How does the visitor pattern simulate multiple dispatch in Java?
- What is the difference between multiple dispatch and method overloading?
- Which languages support native multiple dispatch, and how does Julia implement it?
- Why is single dispatch the default in most mainstream OOP languages?
MCQ Practice
1. Multiple dispatch selects a method implementation based on?
Multiple dispatch examines the runtime types of several arguments together to select the most specific method.
2. Does Java support true multiple dispatch natively?
Java is a single-dispatch language; overload resolution is based on declared (compile-time) argument types, not runtime types of all arguments.
3. What pattern is commonly used to simulate multiple dispatch in single-dispatch languages?
The visitor pattern uses two single-dispatch calls (accept then visit) to achieve effective double dispatch.
Flash Cards
Multiple dispatch in one line? — Method selection based on the runtime types of more than one argument.
Does Java support it natively? — No — Java is single-dispatch; overloads resolve at compile time by declared types.
How is it simulated in Java? — Via the visitor pattern (double dispatch) or instanceof chains.
Example language with native support? — Julia (and Common Lisp via CLOS).