Dynamic binding == late binding in Java or not?
Asked Answered
D

1

7

In different sources i've read different things on the topic. For example Wikipedia says:

Late binding is often confused with dynamic dispatch, but there are significant differences.

But a couple lines later

it is popular to use the term late binding in Java programming as a synonym for dynamic dispatch. Specifically, this refers to Java's single dispatch mechanism used with virtual methods.

So where's the truth and what are this "significant differences"?

Dyane answered 27/2, 2012 at 19:14 Comment(2)
Are you referring to Lazy loading?Crossways
@bmoran: No. He's talking about a more fundamental language level thing. See en.wikipedia.org/wiki/Late_binding#Late_binding_in_Java and en.wikipedia.org/wiki/Dynamic_dispatch .Infielder
M
8

Late binding and dynamic single dispatch are, for all intents and purposes, the same. In dynamic single dispatch, the value or identity of a single object determines which code is invoked at runtime, and that's what happens in Java.

The term dynamic dispatch in general is most often used to imply dynamic multiple dispatch, which is where the runtime method is chosen at runtime based on the identities or values of more than one object, which is a language feature in CLOS and Smalltalk, but not in Java or C++.

Mertiemerton answered 27/2, 2012 at 19:27 Comment(7)
+1, but I'd characterize "late binding" as the fully late bound invocation of a language like Python, while Java's model of dynamic single dispatch is actually a partially-early, partially-late binding, much more like C++ virtual methods.Noninterference
I had assumed that invoke dynamic added in Java 7 performed dynamic dispatch. java.sun.com/developer/technicalArticles/DynTypeLang What is the difference?Completion
@DanielPryden -- indeed, I'd agree with that. What you're calling "fully late bound," I call "duck typing"; this is basically method-based rather than type-based lookup.Mertiemerton
@PeterLawrey -- yeah, that's "duck typing". It's being able to send a message at runtime without any requirements on the type of the recipient. The Java language itself doesn't include this concept, but the Java 7 JVM does support it (and Java now weakly supports it through reflection-like API classes.)Mertiemerton
@ErnestFriedman-Hill: Yeah, I think "late binding" (in the fullest sense) and "duck typing" are basically the same thing. The only distinction I might make between them is that I think of "late binding" as a language feature, while "duck typing" is a more abstract thing, basically just a programming mindset. But I'll admit that that's a pretty fine hair to split.Noninterference
@ErnestFriedman-Hill Could you please make an example of dynamic multiple dispatch in Smalltalk?Egmont
Smalltalk does not have multiple dynamic dispatch to my knowledge, only single.Bromo

© 2022 - 2024 — McMap. All rights reserved.