2

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.

Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178

1 Answers1

4

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.

sea-rob
  • 6,841
  • 1
  • 24
  • 47
  • Thanks for replying. I still don't understand the differences between dynamic binding and dynamic dispatch. Could you explain how they differ from each other? – Aviv Cohn Apr 12 '14 at 14:45
  • 1
    Sorry, I clicked "send" too soon. I added some more text -- does that answer your question? – sea-rob Apr 12 '14 at 14:47
  • "Binding" is the idea that the method is "bound" to a particular instance: As far as I know, binding refers to a method name being bound to a method implementation. – Giorgio Aug 10 '14 at 21:20
  • How would you describe the situation (assume Java or .NET) where a base type includes `Foo(Animal)`, derived type overrides `Foo(Animal)` but also defines `Foo(Cat)`, `baseTypeReference` identifies a derived-type instance, client code calls `baseTypeReference(someCat)`? I would describe that as using static binding but dynamic dispatch. Would you agree with such terminology? – supercat Apr 20 '15 at 17:02