views:

3532

answers:

4

Binding times can be classified between two types: static and dynamic. What is the difference between static and dynamic binding?

Could you give a quick example of each to further illustrate it?

+8  A: 

In the most general terms, static binding means that references are resolved at compile time.

Animal a = new Animal();
a.Roar(); // The compiler can resolve this method call statically.

Dynamic binding means that references are resolved at run time.

public void MakeSomeNoise(object a) {
   // Things happen...
   ((Animal) a).Roar(); // You won't know if this works until runtime!
}
John Feminella
Not true, if object a = new Animal();
David Pokluda
True, although I think it was obvious I was trying to be general. I adjusted it a bit.
John Feminella
+1  A: 

It depends when the binding happens: at compile time (static) or at runtime (dynamic). Static binding is used when you call a simple class method. When you start dealing with class hierarchies and virtual methods, compiler will start using so called VTABLEs. At that time the compiler doesn't know exactly what method to call and it has to wait until runtime to figure out the right method to be invoked (this is done through VTABLE). This is called dynamic binding.

See Wikipedia article on Virtual tables for more details and references.

David Pokluda
A: 

function call at compile time called static binding and function call at run time called dynamic bynding.

Praveen
A: 

yr deakho mainu kuj nhi pta .............................

jash