ELI5: Explain Like I'm 5

Double dispatch

Okay kiddo, imagine you and your friend want to play a game, but you don't know what game to play yet. You both suggest different games, but how will you decide which one to play?

Double dispatch is like that. It's a way for computer programs to decide which action to take based on the type of different objects involved.

Let's say we have two classes: Dog and Cat. Both of them have a method called "speak()", but the way they speak is different. We also have a class called Person that wants to talk to the animals.

So, the Person wants to talk to the dog and the cat, but they need to know which animal they are talking to in order to respond properly. This is where double dispatch comes in.

First dispatch: The Person asks the animal to speak.
Second dispatch: The animal checks who is speaking with them and responds with the appropriate action.

So, it's like when you and your friend suggest games, but your friend says "I want to play a game like tag" and you say "I want to play a game like hide-and-seek". You both suggest a specific game based on what the other person wants to play, and then finally decide on one game to play together.

That's double dispatch! It lets computer programs figure out what actions to take based on the type of objects involved.
Related topics others have asked about: