Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between static and dynamic binding?

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?

like image 754
KingNestor Avatar asked Mar 13 '09 00:03

KingNestor


People also ask

What is the difference between static and dynamic binding Java?

In Java static binding refers to the execution of a program where type of object is determined/known at compile time i.e when compiler executes the code it know the type of object or class to which object belongs. While in case of dynamic binding the type of object is determined at runtime.

What are the differences between static binding and late binding?

Static binding cannot fail at runtime, it is a compile time error to fail to determine what a statically bound reference refers to. Late binding: This is where the exact item being referred is determined at runtime.

What is difference between static method and dynamic method?

The Static Method Thesis states that the scientific method is unchangeable and therefore transhistorical, whereas the Dynamic Method Thesis states that the scientific method is changeable and may vary between historical episodes and communities.

What is the difference between static and dynamic binding in C#?

Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.


2 Answers

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! } 
like image 79
John Feminella Avatar answered Sep 28 '22 14:09

John Feminella


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.

like image 28
David Pokluda Avatar answered Sep 28 '22 15:09

David Pokluda