Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Static binding and dynamic binding are fundamental concepts in object-oriented programming languages like Java, C++, and Python. They dictate how methods are associated with method calls during program execution. Static binding occurs at compile-time, linking method calls based on the type of the reference variable, while dynamic binding occurs at runtime, allowing method invocation based on the actual object type. This blog explores these concepts, their differences, and practical examples to illustrate their significance in software development.
What is Binding?
Binding refers to the linking between the function call and the function definition. In the code, at any point of time when a function is called, then the program control binds to the address in memory where the function has been defined.
Let’s take an example:
C++ code for demonstrating binding:
#include <iostream>
using namespace std;
class A
{
public:
void m1()
{
cout << "m1 is invoked\n";
}
void m2()
{
cout << "m2 is invoked\n";
}
};
int main()
{
A obj;
obj.m1();
obj.m2();
return 0;
}
Output -
m1 is invoked
m2 is invoked
Explanation -
Suppose if we have a class A, which has two functions, m1 and m2 and their definitions. So, to identify for which particular function call, which function is to be executed, binding is needed.
Why Binding Matters in Object-Oriented Programming
Binding in object-oriented programming refers to the process of connecting a method call to its definition. It's crucial for enabling polymorphism and inheritance, especially in Java. With dynamic binding (also known as late binding), the JVM determines at runtime which method to invoke, allowing subclasses to override methods and customize behavior.
This makes code more flexible and extensible, as developers can write general code using parent classes while the actual behavior is defined in child classes. Binding also supports method overriding, ensuring the correct version of a method runs based on the object type.
While dynamic binding may introduce slight overhead compared to static binding, it greatly improves code readability, reusability, and maintainability, making it essential for designing robust and scalable Java applications.
What is Static Binding?
Static binding happens at compile-time, i.e., the function call and the function definition are linked during the compile time itself. So, it is also called early binding.
This is because all the information needed to associate the function call to its definition is available at the compile time.
When does static binding take place?
Static binding happens by default for any normal function calls in the program. It also occurs during function overloading and operator overloading.
The binding of static, private and final methods is always compile-time. Why? This is because the type of class is determined at the compile-time and hence binding happens then and there.
Let's look at an example to understand better -
Java code for demonstrating Static binding:
class add {
public int sum(int a, int b) {
return a + b;
}
public int sum(int a, int b, int c) {
return a + b + c;
}
}
public class static_binding {
public static void main(String args[]) {
add a1 = new add();
int a, b, c;
a = 10;
b = 20;
c = 30;
System.out.println(a1.sum(a, b));
System.out.println(a1.sum(a, b, c));
}
}
You can also try this code with Online Java Compiler
#include <iostream>
using namespace std;
class findSum
{
public:
//The function sum() is overloaded in this class.
int sum(int a, int b)
{
return a + b;
}
int sum(int a, int b, int c)
{
return a + b + c;
}
};
int main()
{
findSum obj;
int a, b, c;
a = 10;
b = 20;
c = 30;
cout << "The sum is " << obj.sum(a, b) << endl;
cout << "The sum is " << obj.sum(a, b, c) << endl;
return 0;
}
You can also try this code with Online C++ Compiler
In the above code, in the class findSum(), we see that the function sum() is overloaded. There are two definitions of sum, and the only difference is in the number of parameters passed.
When we call the same function sum() from the main, it gets bound to the correct definition. So, in this case, at compile time itself, the function call binds to the correct function depending upon the parameters passed to them.
What is Dynamic Binding?
Dynamic binding takes place during run time based on the type of object. Since it is delayed till the run time, it is also called late binding or runtime binding. When the compiler cannot determine all the information required to resolve a function call during compile time, these function calls are not bound until run time.
When does dynamic binding take place?
It can be achieved with the use of virtual functions in C++ and overridden methods in Java.
Example of Dynamic binding:
Java code for demonstrating Dynamic binding:
class Human {
// Overridden Method
public void walk() {
System.out.println("Human walks");
}
}
class Boy extends Human {
// Overriding Method
public void walk() {
System.out.println("Boy walks");
}
}
public class dynamic_binding {
public static void main(String args[]) {
// Reference is of Human type and object is Boy type
Human obj = new Boy();
obj.walk();// calls function of Boy class
// Reference is of Human type and object is of Human type Human
obj = new Human();
obj.walk();// calls function of Human class
}
}
You can also try this code with Online Java Compiler
// C++ code to demonstrate the concept of dynamic binding
#include <iostream>
using namespace std;
class B
{
public:
// function f() is declared as virtual in the base class
virtual void f()
{
cout << "The base class function is called.\n";
}
};
class D : public B
{
public:
void f() //function overriding
{
cout << "The derived class function is called.\n";
}
};
int main()
{
B base; // base class object
D derived; //derived class object
B *basePtr = &base; // base class pointer pointing to base class object
basePtr->f(); //calls function of base class
basePtr = &derived; // base class pointer pointing to object of derived class
basePtr->f(); //calls function of derived class
return 0;
}
You can also try this code with Online C++ Compiler
The base class function is called.
The derived class function is called.
Explanation:
In the above example, we have a base class B in which function f() has been declared with the “virtual” keyword. The derived class D inherits from the base class B, which means all the member functions of class B will also be available in class D. So, the function f() of class B will be accessible from class D.
But we see that the function f() has been defined again in the derived class.
Now, in the main code, we have a base class pointer basePtr. Firstly it points to a base class object “base”, and the function call basePtr->f() calls the function f() of the base class, which is expected also because the pointer, as well as the object to which it points, are both of the type base class.
But when basePtr points to the derived class object “derived”, it is important to note that the pointer is of type base class and the object it points to is of type derived class. The function calls basePtr->f() whichgets resolved at run time, and hence the function f() of the derived class is executed as the object is of the derived class. This is an example of function overriding, which is possible because of dynamic binding/late binding.
Use Cases and Real-Life Scenarios of Binding in Java
Binding is central to how Java executes methods, and it plays a key role in both compile-time and runtime behavior. Understanding static and dynamic binding helps developers write efficient and flexible code.
When to Use Static Binding
Static binding (or early binding) occurs at compile time. It is used when the method type is private, final, or static, or in cases of method overloading. Since the compiler knows the exact method to call, static binding results in faster and more predictable execution.
Use Case 1: Utility Classes
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
You can also try this code with Online Java Compiler
The compiler decides which print method to call based on the parameter type, making it a static binding scenario.
Benefits:
Better performance due to compile-time resolution
Predictable behavior
Ideal for helper methods, utility functions, and cases where method behavior doesn't change
When to Use Dynamic Binding
Dynamic binding (or late binding) happens at runtime and is used when calling overridden methods through a parent class reference. It enables polymorphism, allowing Java to choose the appropriate method implementation at runtime based on the object type.
Use Case: Polymorphism with Inheritance
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Output: Bark
}
}
You can also try this code with Online Java Compiler
Useful in frameworks, event handling, and plugin architectures
Dynamic binding enables runtime decision-making, making Java applications more adaptable and maintainable. It's a powerful tool in designing loosely coupled, scalable systems.
Frequently Asked Questions
What is static and dynamic binding in OOP?
In static binding, function calls are resolved at compile time by the compiler itself. The binding of all the static and private functions/methods of a class happens at compile time. In dynamic binding, function calls are resolved at run time. Function overriding in OOP is possible due to dynamic/late binding.
What is an example of dynamic binding?
Function overriding is an example of dynamic binding.
What are dynamic binding and message passing?
Dynamic binding links the function call to the function body/definition at run time according to the object type. In contrast, message passing is a method of exchanging information between objects in Object-oriented programming.
What is the difference between static and dynamic methods?
Static methods belong to the class itself and are called using the class name. Dynamic methods are associated with objects and can be overridden in subclasses, allowing for polymorphic behavior based on the object's type.
What is the difference between static binding and late binding?
Static binding, or early binding, resolves method calls at compile-time based on the reference type. Late binding, or dynamic binding, determines method execution at runtime based on the actual object type, facilitating polymorphism and flexibility in method invocation.
Conclusion
In this article, we learned in detail about binding and its types - static and dynamic binding. We also discussed static vs dynamic binding and compared them based on when these are achieved and their advantages and disadvantages.
This topic comes under the Object-oriented programming paradigm and is often asked during technical interviews.
So, having a clear understanding of this will help you master other related concepts also easily.