Dynamic Dispatch - Single and Multiple Dispatch

Single and Multiple Dispatch

Dynamic dispatch is needed when multiple classes contain different implementations of the same method (for example foo). If the class of an object x is not known at compile-time, then when x.foo is called, the program must decide at runtime which implementation of foo to invoke, based on the runtime type of object x. This case is known as single dispatch because an implementation is chosen based on a single type—that of the type of the instance. Single dispatch is supported by many object-oriented languages, including statically typed languages such as C++ and Java, and dynamically typed languages such as Smalltalk and Objective-C.

In a small number of languages such as Common Lisp and Dylan, methods or functions can also be dynamically dispatched based on the type of arguments. Expressed in pseudocode, the code manager.handle(y) could call different implementations depending on the type of object y. This is known as multiple dispatch.

Read more about this topic:  Dynamic Dispatch

Famous quotes containing the words single and/or multiple:

    The shore is composed of a belt of smooth rounded white stones like paving-stones, excepting one or two short sand beaches, and is so steep that in many places a single leap will carry you into water over your head; and were it not for its remarkable transparency, that would be the last to be seen of its bottom till it rose on the opposite side. Some think it is bottomless.
    Henry David Thoreau (1817–1862)

    Creativity seems to emerge from multiple experiences, coupled with a well-supported development of personal resources, including a sense of freedom to venture beyond the known.
    Loris Malaguzzi (20th century)