Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between Late binding and Early Binding.  RSS feed

 
karthikeya kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difference between Late binding and Early Binding. Is there any difference between Dynamic polymor and Late binding. Can anyone give me an example
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't say early binding. Say compile‑time binding or static binding. That will make the concept easier to understand. All static methods are bound at compile‑time in Java┬«, so if you say…, the compiler will choose which method to use, that in the Animal class, and you will get the same result irrespective of whether you have a dog or a cat. Incidentally, that is why you should call static methods by ClassName.method not objectName.method.
As you will find from this FAQ, static methods always use compile‑time binding. Fields also use compile‑time binding.
For late binding, come back in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure you would find something better in Wikipedia or similar.

Don't say late binding. Say runtime binding or dynamic binding. That applies to instance methods, and nothing else. Instance methods can be overridden, but nothing else can be overridden. Instance methods are sought dynamically. The opposite of dynamic is static; things checked at compile‑time are not dynamically bound and that is probably why static methods are called static.At compile time, all that is checked is the existence of a method with that name in the Animal class. That will ensure that Dog and Cat both have a correct version of the method, too. At runtime, there is dynamic binding (see, easier name to understand than late binding) or runtime binding (the name reminds you how it works), and the runtime looks in the associated Class<T> object to look for the method. If it finds it, the method has been overridden and it uses that version. If not, the method has not been overridden and the runtime looks in superclasses' Class<T> objects until it finds the (not overridden) method. That is how polymorphism is implemented. Read more about polymorphism here. Runtime binding means you do not need to know the actual type of the object beforehand:-You will get the correct overriding of that method for a Lion or a Wildebeest even though you do not know which it will be.


Warning: The different types of binding are different for different object‑oriented languages. I think (but I am not sure) that the different kinds of binding apply only to object‑oriented programming.

[edit]: Another warning. What I said has been very much simplified.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!