Updated March 18, 2023
Introduction to Static Binding and Dynamic Binding
Static Binding and Dynamic Binding are two types of binding. Binding refers to the association of method call to method body. In Static binding, all the assignment & declaration happens at the time of compilation. They have multiple pros and cons over each other. In performance, Static Binding is better than the dynamic binding of the methods & variables. Static Binding is also known as Early Binding. As all we know, methods Binding like static, private & final happens over compile time while in the dynamic binding compiler doesn’t decide which method will be called.
Examples
Below given examples are the better examples to understand:
Example #1
public class StaticBindingExample {
public static void main(String args[]) {
Vehicle veh = new MotorBike();
veh.start();
}
}
class Vehicle {
static void start() {
System.out.println("vehicle will start now..");
}
}
class MotorBike extends Vehicle {
static void start() {
System.out.println("Bike will start now..");
}
}
In the above-given example, we can see how static binding is intended to prevent class overload.
Below, an example will display how Dynamic Binding works; here, the child class method is overriding the parent class.
Example #2
public class StaticBindingExample {
public static void main(String args[]) {
Vehicle veh = new MotorBike();
veh.start();
}
}
class Vehicle {
void start() {
System.out.println("vehicle will start now..");
}
}
class MotorBike extends Vehicle {
void start() {
System.out.println("Bike will start now..");
}
}
In this example, static is not present in the start method under the parent & child class method; thus, the method with the same name will override the parent class method. It happens due to this.
In the above-given screenshot, the output of the program is given.
Why to use it?
- It is necessary for the programming to handle different situations by the use of Static & Dynamic Binding.
- Compile-time Binding is known as Early Binding, i.e. when needed to declare things at the time of compilation (in the very beginning) of the program execution. In Dynamic Binding, Binding takes place at the run time that is also known as Late Binding.
- Method & variables, defined as static, final & private, refers to the Static Binding while virtual methods which Binding takes place at run-time are known as Dynamic Binding.
- Method Overloading/ Operator Overloading uses Static Binding so it is one of the best examples of it, while Overriding uses Dynamic, so it is known as the best example of Dynamic Binding.
- In this, a Real object is never used while a real object used in the Dynamic Binding.
Other Examples
In the below-given example, we can see how static binding happens.
Example #1
//Parent class implementation
Code:
class Colony{
//Static method to start the electricity of the Colony
public static void start electricity(){
//printing the message here
System.out.println("Colony Electricity is On.");
}
}
//Child class implementation
class Building extends Colony{
//Static method to start the electricity of Building
public static void startElectricity(){
//printing the message here
System.out.println("Building Electricity is On.");
}
}
public class StaticBindingExample {
public static void main(String args[]) {
//Creating object of the parent class type
Colony col = new Colony();
col.startElectricity();
//Creating object of the child class type
Colony col2 = new Building();
col2.startElectricity();
}
}
In the above-given program, col is the reference variable of type class Colony, pointing to class Colony’s object. col2 is also the reference variable of type class Colony but pointing to the object of class Building. When the compilation takes place, while the binding compiler never checks the type of object, it just checks the type of reference variable.
In the above program, such as col .startElectricity(), the compiler checks whether the start electricity() method definition exists in the class Colony or not because col is class Colony type, similarly for col2.startElectricity() method call. It checks whether the start electricity() method is present in class Colony or not because col2 is also the class Colony type. It doesn’t check to which object col, col2 is pointing. This type of binding is known as Static Binding.
Output:
The above-given output of the program shows how static members are being used to prevent overriding.
Example #2
In the below-given example, we can see how Dynamic Binding occurs during run time.
//Parent class implementation
Code:
class Colony{
//method to start the electricity of the Colony
void startElectricity(){
//printing the message here
System.out.println("Colony Electricity is On.");
}
}
//Child class implementation
class Building extends Colony{
//method to start the electricity of Building
void startElectricity(){
//printing the message here
System.out.println("Building Electricity is On.");
}
}
public class StaticBindingExample {
public static void main(String args[]) {
//Creating object of the parent class type
Colony col = new Colony();
col.startElectricity();
//Creating object of the child class type
Colony col2 = new Building();
col2.startElectricity();
}
}
In the above-given program, everything is the same as in a static example program, but in this example member of the method “static” is removed. During this, the method of the main object to which col.startElectricity() is pointing will be called. While for col2.startElectricity() call, the method of the main object to which col2 is pointing will be called. This type of binding is known as Dynamic Binding.
Output:
Advantages
Below are some of the advantages given.
- Static Binding execution is more efficient & faster than Dynamic. This Binding compiler knows that these types of methods can not be overridden.
- In the Static Binding, the type is used while Dynamic Binding uses objects for Bindings.
- One of the major advantages of Dynamic Binding is flexibility; due to the flexibility, a single function can handle different types of an object at runtime.
- In Static Binding, All information needed before the compilation time, while in Dynamic Binding, no information remains available before run time.
- Static Binding can take place using normal functions, while Dynamic Binding can be achieved using virtual functions.
Conclusion – Static Binding and Dynamic Binding
These are some of the important concepts of object-oriented programming. Binding refers to the execution of the code. It happens at the time of compile, while Dynamic Binding happens at the run time. Definition, Declaration & Scope of declaration, these are three important pillars of Static Binding while in Dynamic Binding, name of Binding & lifetime of binding matters.
Recommended Articles
This is a guide to Static Binding and Dynamic Binding. Here we discuss the introduction, uses, examples and advantages, respectively. You can also go through our other suggested articles to learn more –