Static Binding VS Dynamic Binding

Static Binding VS Dynamic Binding
Static Binding VS Dynamic Binding

Introduction

In this article, we are going to learn static vs dynamic binding. We will see each of them in detail and then compare them based on their functionalities and where they are used.

We know that Object-Oriented Programming is useful in many ways in terms of the flexibility it provides while creating classes, calling functions etc. There is a concept of function overloading and function overriding. So, if you have always wanted to know what happens behind the scenes while implementing these features, then you are at the right place. This article will provide you with an in-depth understanding of static vs dynamic binding concepts in an easy and precise manner with examples to give you a clearer picture.

Let’s get started with the question – What is Binding?

Binding refers to the linking between the function call and the function definition.

In your 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 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. 

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));

    }
}

Output:

30
60

C++ code for demonstrating Static binding:

#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;
}

Output:

The sum is 30
The sum is 60

Explanation:

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. 

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

    }
}

Output:

Boy walks
Human walks

C++ code for demonstrating Dynamic binding:

// 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;
}

Output:

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() which gets 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.

Static Vs Dynamic Binding 

Static BindingDynamic Binding
It happens at the compile time.It happens at the run time.
It is also called early binding.It is also called late binding.
When all the information needed to call a function is available at the compile time, static binding occurs.When the compiler cannot determine all the information to resolve the function call, dynamic binding occurs.
It can be achieved during normal function calls, function overloading and operator overloading.It is achieved with the use of virtual functions.
Execution becomes faster than dynamic binding because the function call gets resolved before run time.As the function call is resolved at run time, sometimes it leads to slower code execution.
It provides less flexibility compared to the dynamic binding. It provides more flexibility as different types of objects at runtime can be handled by a single function call making the source code more readable.

Frequently Asked Questions

What is static and dynamic binding in OOP?

n 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.

Can we override the static method?

No, the static methods cannot be overridden because static methods are bound at compile time using static binding. Example – If a derived class D defines a static method with the same signature as the static method defined in the base class, the base class method gets executed. Hence, no overriding takes place.

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.

Key Takeaways

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. 

Read more about Object-oriented Programming here. You can also have a look at the commonly asked OOPs interview questions to get a better understanding.

By: Yukti Kumari