+ -

Pages

Sunday, June 29, 2014

Static bindings Vs Dynamic bindings in Java


Hi guys, how are you? hope you all are doing well. Today I would like to write something about Static binding Vs Dynamic bindings in Java. First of all what does mean by binding?
Connecting a method call to the method body is known as method binding...
So, there are two types of bindings,
  • Dynamic bindings AKA Late bindings
  • Static bindings AKA Early bindings

# Dynamic bindings {Late bindings}

When we call a method in java it'll resolve at run time or compile time. It depends on whether that our method is a virtual method  or a static method. If our method call is resolved at run time, it's a Dynamic binding. How does it happen? Let's take an example.


We know Java supports Polymorphism. So a reference variable of type Parent can hold an object of type Child which extends Parent. In above example, type of human is Human, but it holds object of type Boy. Now if you call a virtual method on this object, the compiler can't identify correct method. Because it can be the defined method in Human{Parent} or it can be the overridden method in Boy{Child}. So this call will resolve at run time. Because at run time actual object will be available. This is the behavior of Dynamic binding.

# Static bindings {Early bindings}

If our method call is resolved at compile time, it's a static binding. Basically all the private, final, static variables and methods will be resolved at compile time. Because they can't be overridden. So compiler can clearly understand who will be called using this reference variable. An example for static binding,


In this example, type of reference variable myDog is Dog and it holds object of Dog. Actually it's like it holds it self. There is no confusion for compiler. But eat method is not a  private, final or a static. But it's qualified to resolve at compile time. Because it's not overridden. This is the behavior of Dynamic binding.

This is my very first technical blog post. Hope this'll help you at least a little bit :) Like to hear from you. If you have any ideas, comments, doubts please comment here. See you soon. Bye Bye...
    5 {foss}-4ever: Static bindings Vs Dynamic bindings in Java Hi guys, how are you? hope you all are doing well. Today I would like to write something about Static binding Vs Dynamic bindings in Ja...

    4 comments:

    < >