Table of Contents
Connecting a method call to the method body is known as binding.
There are two types of binding
- Static Binding (also known as Early Binding).
- Dynamic Binding (also known as Late Binding).
Understanding Type
Let’s understand the type of instance.
1) variables have a type
Each variable has a type, it may be primitive and non-primitive.
1. int data=30;
Here data variable is a type of int.
Ace your coding skills with Entri !
2) References have a type
- class Dog{
- public static void main(String args[]){
- Dog d1;//Here d1 is a type of Dog
- }
- }
Grab the opportunity to learn Java with Entri! Click Here
3) Objects have a type
An object is an instance of particular java class,but it is also an instance of its superclass. |
- class Animal{}
- class Dog extends Animal{
- public static void main(String args[]){
- Dog d1=new Dog();
- }
- }
Here d1 is an instance of Dog class, but it is also an instance of Animal. |
Ace your coding skills with Entri !
Static binding
When type of the object is determined at compiled time(by the compiler), it is known as static binding.
If there is any private, final or static method in a class, there is static binding.
Example of static binding
- class Dog{
- private void eat(){System.out.println(“dog is eating…”);}
- public static void main(String args[]){
- Dog d1=new Dog();
- d1.eat();
- }
- }
Grab the opportunity to learn Java with Entri! Click Here
Dynamic binding
1: What is the default value of a boolean in Java?
When type of the object is determined at run-time, it is known as dynamic binding.
Example of dynamic binding
- class Animal{
- void eat(){System.out.println(“animal is eating…”);}
- }
- class Dog extends Animal{
- void eat(){System.out.println(“dog is eating…”);}
- public static void main(String args[]){
- Animal a=new Dog();
- a.eat();
- }
- }
Output:dog is eating...
Ace your coding skills with Entri !
Difference Between Static Binding and Dynamic Binding
Binding association of a ‘function definition’ to a ‘function call’ or an association of a ‘value’ to a ‘variable’, is called ‘binding’. During compilation, every ‘function definition’ is given a memory address. As soon as function calling is done, control of program execution moves to that memory address and get the function code stored at that location executed, this is Binding of ‘function call’ to ‘function definition’. Binding can be classified as ‘static binding’ and ‘dynamic binding’.
If it’s already known before runtime, which function will be invoked or what value is allotted to a variable, then it is a ‘static binding’. if it comes to know at the runtime, then it is called ‘dynamic binding’.
Grab the opportunity to learn Java with Entri! Click Here
Static Binding Vs Dynamic Binding
Comparison chart
BASIS FOR COMPARISON | STATIC BINDING | DYNAMIC BINDING |
---|---|---|
Event Occurrence | Events occur at compile time are “Static Binding”. | Events occur at run time are “Dynamic Binding”. |
Information | All information needed to call a function is known at compile time. | All information need to call a function come to know at run time. |
Advantage | Efficiency. | Flexibility. |
Time | Fast execution. | Slow execution. |
Alternate name | Early Binding. | Late Binding. |
Example | Overloaded function call, overloaded operators. | Virtual function in C++, overridden methods in java. |
Ace your coding skills with Entri !
Definitions of Static Binding
When compiler acknowledges all the information required to call a function or all the values of the variables during compile time, it is called “static binding“. As all the required information are known before runtime, it increases the program efficiency, and it also enhances the speed of execution of a program.
Static Binding makes a program very efficient, but it declines the program flexibility, as ‘values of the variable’ and ‘function calling’ are predefined in the program. Static Binding is implemented in a program at the time of coding.
Overloading a function or an operator are the example of compile-time polymorphism, i.e. static Binding.
Implementation
Now, here the statement’ O1.loads(20)’ clearly binds the function calling to first ‘load(int x)’ function as it is the only function which accepts single integer argument. The statement O1.loads(20,40) binds the function calling to second ‘load(int x, int y)’ function as it is the only function which accepts two integer arguments. Hence, no time will be wasted in deciding which function to invoke; this will make program execution efficient and fast.
Grab the opportunity to learn Java with Entri! Click Here
Definition of Dynamic Binding
Calling a function or assigning a value to a variable, at runtime is called “Dynamic Binding“. Dynamic Binding can be associated with run time ‘polymorphism’ and ‘inheritance’ in OOP.
Dynamic Binding makes the execution of program flexible as it can be decided, what value should be assigned to the variable and which function should be called, at the time of program execution. However, as this information is provided at runtime, it makes the execution slower as compared to static Binding.
Implementation
Here the value of the pointer changes as the program is in execution and the value of the pointer decides which class’s function will be invoked. So here, the information is provided at run time, it takes the time to bind the data which slowdowns the execution.
Ace your coding skills with Entri !
Key Differences Between Static and Dynamic Binding
- Events that occur at compile time like, a function code is associated with a function call or assignment of value to a variable, are called static/early Binding. On the contrary, when these tasks are accomplished during runtime, they are called dynamic/late Binding.
- ‘Efficiency’ increases in static Binding, as all the data is gathered before the execution. But in dynamic Binding, the data is acquired at runtime so we can decide what value to assign a variable and which function to invoke at runtime this makes execution ‘flexible’.
- ‘Static binding’ make the execution of a program ‘faster’ as all the data needed to execute a program is known before execution. In ‘dynamic binding’ data needed to execute a program is known to the compiler at the time of execution which takes the time to bind values to identifiers; hence, it makes program execution slower.
- Static Binding is also called early Binding because the function code is associated with function call during compile time, which is earlier than dynamic Binding in which function code is associated with function call during runtime hence it is also called late binding.
Grab the opportunity to learn Java with Entri! Click Here
Conclusion
However, we conclude that when we have prior knowledge of the values of variable and function calling, we apply the static binding. Conversely, in dynamic Binding, we provide all the information at the time of execution.