What is the difference between static and dynamic binding?

64,196

Solution 1

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!
}

Solution 2

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.

Solution 3

I came accross this perfect answer of a quora user "Monis Yousuf". He explain this perfectly. I am putting it here for others.

Binding is mostly a concept in object oriented programming related to Polymorphism.

Firstly, understand what Polymorphism is. Books say that it means "one name and multiple forms". True, but too abstract. Let us take a real-life example. You go to a "Doctor", a doctor may be an eye-specialist, ENT specialist, Neuro-Surgeon, Homeopath etc.

Here, a "doctor" is a name and may have multiple types; each performing their own function. This is polymorphism in real life.

Function Overloading: This concept depicts Static Binding. Function overloading may be roughly defined as, two or more methods (functions) which have the same name but different signatures (including number of parameters, types of parameters, differt return types) are called overloaded methods (or functions).

Suppose you have to calculate area of a rectangle and circle. See below code:-

class CalculateArea {

    private static final double PI = 3.14;

    /* 
       Method to return area of a rectangle 
       Area of rectangle = length X width
    */
    double Area(double length, double width) {
        return (length * width);
    }

    /*
      Method to return area of circle
      Area of circle = π * r * r
    */
    double Area(double radius) {
        return PI * radius * radius;
    }
}

In above code, there are two methods "Area" with different parameters. This scenario qualifies as function overloading.

Now, coming to the real question: How is this static binding?

When you call any of the above functions in your code, you have to specify the parameters you are passing. In this scenario, you will pass either:

  • Two parameters of type double [Which will call the first method, to calculate are of a rectangle]
  • Single parameter of type double [Which will call the second method, to calculate area of a circle]

Since, at compile time the java compiler can figure out, WHICH function to call, it is compile-time (or STATIC) binding.

Function Overriding: Function overriding is a concept which is shown in inheritance. It may roughly be defined as: when there is a method present in a parent class and its subclass also has the same method with SAME signature, it is called function overriding. [There is more to it, but for the sake of simplicity, i have written this definition] It will be easier to understand with below piece of code.

class ParentClass {
    int show() {
        System.out.println("I am from parent class");
    }
}

class ChildClass extends ParentClass{
    int show() {
        System.out.println("I am from child class");
    }
}

class SomeOtherClass {
    public static void main (String[] s) {
        ParentClass obj = new ChildClass();
        obj.show();
    }
}

In above code, the method show() is being overridden as the same signature (and name) is present in both parent and child classes.

In the third class, SomeOtherClass, A reference variable (obj) of type ParentClass holds the object of ChildClass. Next, the method show() is called from the same reference variable (obj).

Again, the same question: How is this Dynamic Binding?

At compile time, the compiler checks that the Reference variable is of type ParentClass and checks if the method show() is present in this class. Once it checks this, the compilation is successful.

Now, when the programs RUNS, it sees that the object is of ChildClass and hence, it runs the show() method of the ChildClass. Since this decision is taken place at RUNTIME, it is called Dynamic Binding (or Run-time Polymorphism).

Link for original answer

Share:
64,196
KingNestor
Author by

KingNestor

CS Student at the University of Central Missouri

Updated on January 09, 2020

Comments

  • KingNestor
    KingNestor over 4 years

    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?