Comparison
Dynamic dispatchvsPolymorphism
Dynamic dispatch
the method that runs is chosen by what the object actually is at that moment, not by the type the variable was declared with.
The runtime lookup that decides which implementation a call lands on, based on the receiving object rather than the compile-time type. It is what makes polymorphism work and what makes a call slightly slower and harder for a compiler to inline. Knowing it is dispatch rather than magic is what lets you reason about why a base-class method calling an overridden method runs the subclass version — the source of most surprises in a deep hierarchy.
Full entry →Polymorphism
the same call site does three different things depending on what was passed in, and there is no branch anywhere.
One name standing for behaviour that varies by the runtime type of the thing it is called on. It is the mechanism underneath most of the behavioural patterns — strategy, state, visitor and template method are all polymorphism with a particular story attached. The reason interviewers keep asking about it is that the alternative, a conditional over a type tag, is what most real code does instead, and the difference shows up on the day a fourth case is added.
Full entry →