Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Polymorphism and Multiple Dispatch?

...or are they the same thing? I notice that each has its own Wikipedia entry: Polymorphism, Multiple Dispatch, but I'm having trouble seeing how the concepts differ.

Edit: And how does Overloading fit into all this?

like image 652
raldi Avatar asked Sep 24 '08 02:09

raldi


People also ask

Is multiple dispatch polymorphism?

The language is exciting, science-geared, and fun to use with its unique paradigm and programming style. To understand the Julia programming language, you need to understand multiple dispatch. Multiple dispatch is a generic programming concept that is built on the grounds of parametric polymorphism.

What is the difference between polymorphism and dynamic dispatch?

A polymorphic operation has several implementations, all associated with the same name. Bindings can be made at compile time or (with late binding) at run time. With dynamic dispatch, one particular implementation of an operation is chosen at run time.

What is multiple dispatch in Python?

Multiple dispatch (aka multimethods, generic functions, and function overloading) is choosing which among several function bodies to run, depending upon the arguments of a call.


3 Answers

Polymorphism is the facility that allows a language/program to make decisions during runtime on which method to invoke based on the types of the parameters sent to that method.

The number of parameters used by the language/runtime determines the 'type' of polymorphism supported by a language.

Single dispatch is a type of polymorphism where only one parameter is used (the receiver of the message - this, or self) to determine the call.

Multiple dispatch is a type of polymorphism where in multiple parameters are used in determining which method to call. In this case, the reciever as well as the types of the method parameters are used to tell which method to invoke.

So you can say that polymorphism is the general term and multiple and single dispatch are specific types of polymorphism.

Addendum: Overloading happens during compile time. It uses the type information available during compilation to determine which type of method to call. Single/multiple dispatch happens during runtime.

Sample code:

using NUnit.Framework;  namespace SanityCheck.UnitTests.StackOverflow {     [TestFixture]     public class DispatchTypes     {         [Test]         public void Polymorphism()         {             Baz baz = new Baz();             Foo foo = new Foo();              // overloading - parameter type is known during compile time             Assert.AreEqual("zap object", baz.Zap("hello"));             Assert.AreEqual("zap foo", baz.Zap(foo));               // virtual call - single dispatch. Baz is used.             Zapper zapper = baz;             Assert.AreEqual("zap object", zapper.Zap("hello"));             Assert.AreEqual("zap foo", zapper.Zap(foo));               // C# has doesn't support multiple dispatch so it doesn't             // know that oFoo is actually of type Foo.             //             // In languages with multiple dispatch, the type of oFoo will              // also be used in runtime so Baz.Zap(Foo) will be called             // instead of Baz.Zap(object)             object oFoo = foo;             Assert.AreEqual("zap object", zapper.Zap(oFoo));         }          public class Zapper         {             public virtual string Zap(object o) { return "generic zapper" ; }             public virtual string Zap(Foo f) { return "generic zapper"; }         }          public class Baz : Zapper         {             public override string Zap(object o) { return "zap object"; }             public override string Zap(Foo f) { return "zap foo"; }         }          public class Foo { }     } } 
like image 79
jop Avatar answered Sep 28 '22 07:09

jop


With multiple dispatch, a method can have multiple arguments passed to it and which implementation is used depends on each argument's type. The order that the types are evaluated depends on the language. In LISP, it checks each type from first to last. Languages with multiple dispatch make use of generic functions, which are just function declarations and aren't like generic methods, which use type parameters.

Multiple dispatch allows for subtyping polymorphism of arguments for method calls.

Single dispatch also allows for a more limited kind of polymorphism (using the same method name for objects that implement the same interface or inherit the same base class). It's the classic example of polymorphism, where you have methods that are overridden in subclasses.

Beyond that, generics provide parametric type polymorphism (i.e., the same generic interface to use with different types, even if they're not related — like List<T>: it can be a list of any type and is used the same way regardless).

like image 44
Mark Cidade Avatar answered Sep 28 '22 07:09

Mark Cidade


Multiple Dispatch is more akin to function overloading (as seen in Java/C++), except the function invoked depends on the run-time type of the arguments, not their static type.

like image 37
eduffy Avatar answered Sep 28 '22 06:09

eduffy