Object-oriented – Differences between Dynamic Dispatch and Dynamic Binding

bindingdispatchdynamiclanguagesobject-oriented

I've been looking on Google for a clear diffrentiation with examples but couldn't find any.

I'm trying to understand the differences between Dynamic Dispatch and Dynamic Binding in Object Oriented languages.

As far as I understand, Dynamic Dispatch is what happens when the concrete method invoked is decided at runtime, based on the concrete type.

For example:

public void doStuff(SuperType object){
    object.act();
}

SuperType has several subclasses. The concrete class of the object will only be known at runtime, and so the concrete act() implementation invoked will be decided at runtime.

However, I'm not sure what Dynamic Binding means, and how it differs from Dynamic Dispatch.

Please explain Dynamic Binding and how it's different from Dynamic Dispatch. Java examples would be welcome.

Best Answer

Dynamic binding is another name for Late Binding. That's where the language ties a data element or method to an object after compilation time.

Wikipedia equates dynamic & late binding:

  • Dynamic binding (computing), also known as late binding

links to

http://en.wikipedia.org/wiki/Dynamic_binding_(computing)

Javascript was my first exposure to that, because you can just drop functions into objects willy nilly and do cool things with them.

For example (untested, not guaranteed to work exactly):

var a = Object();
var do = function() { do something };
a.do = do;
a.do();
// neato!

BTW, on a side note, there is some question whether Java is Object-oriented, because one of the original ideas in OO was late binding. Unfortunately, this particular discussion seems to devolve into attempts to define "Object Oriented".

http://c2.com/cgi/wiki?IsJavaObjectOriented

lol!

Under the covers, Dynamic Dispatch and Dynamic Binding may work out the same. But the idea in dynamic dispatch is following some function pointer to see which method to actually invoke, or object to invoke it on. "Binding" is the idea that the method is "bound" to a particular instance (or class of instances) & that's how you identify it.

So they could work together -- a method that's bound to an object with dynamic binding might use dynamic dispatch when you call it.

...

Also dynamic dispatch has more of an OO flavor to it... it's the mechanism behind polymorphism, in which a reference to an object might point to one of multiple implementations. Dynamic dispatch decides at runtime which one to actually run. By contrast, late binding would be dropping in whole new methods that weren't there at compile time.