Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Static Binding and Dynamic Binding

Difference Between Static Binding and Dynamic Binding

February 1, 2018 Posted by Lithmee

Key Difference – Static Binding vs Dynamic Binding
 

Programming languages such as Java and C# support Object Oriented Programming (OOP). It allows building software using objects. There are many objects in a software system or a program. These objects have attributes and methods. Attributes describe the characteristics. Methods describe the actions that can be performed by the object. Data is passed through objects using methods. The required values are sent through method calls with parameters. The actual method implementation is in the method definition. There is a link between a method call and method definition. It is known as binding. There are two types of bindings. They are static binding and dynamic binding. The key difference between static binding and dynamic binding is that, in static binding, the binding is resolved at the compile time while dynamic binding is resolved at the run time, which is the actual time of execution. This article discusses the difference between these two binding mechanisms.

CONTENTS

1. Overview and Key Difference
2. What is Static Binding
3. What is Dynamic Binding
4. Similarities Between Static Binding and Dynamic Binding
5. Side by Side Comparison – Static Binding vs Dynamic Binding in Tabular Form
6. Summary

What is Static Binding?

Binding is the link between a method call and method definitions.

Difference Between Static Binding and Dynamic Binding

Figure 01: Static Binding and Dynamic Binding

Refer the below program written in Java.

public class A{

public void method1(){

System.out.println(“Method1”);

}

public void method2(){

System.out.println(“Method2”);

}

public static void main(String[] args){

A obj  = new A();

obj.method1();

obj.method2();

}

}

According to the above program, an object of type A is created. Then method1 and method2 are called. Identifying which method should call for execution is known as binding. Statement obj.method1() will call method1() and obj.method2() will call method2(). This link is binding.

In static binding, binding is resolved at compile time by the compiler. It is also known as early binding. Binding happens before a program actually runs. Static binding occurs in method overloading. Refer the below program written in Java.

public void Calculation{

public void sum(int x, int y){

System.out.println(“Sum is “, x+y);

}

public void sum(double x, double y){

System.out.println(“Sum is “, x+y);

}

public static void main(String[] args){

Calculation cal = new Calculation();

cal.sum(2,3);

cal.sum(5.1,  6.4);

}

}

According to the above program, when passing the two integers, the method with two integers will be invoked. When passing two double values, the method corresponding to two double values will be invoked. This binding process occurs at the time of compilation. The compiler knows that it should call sum method with two integer values for cal.sum(2,3). For cal(5.1,6.4), it will call the sum method with two double values. All required information is known before runtime, so it increases the program efficiency and execution speed.

What is Dynamic Binding?

In Dynamic Binding the compiler does not resolve the binding at compile time. Binding occurs at run time. It is also known as late binding. Dynamic Binding occurs in method overriding.  Refer to program written in Java.

public class Shape(){

public void draw(){

System.out.println(“Draw shape”);

}

}

public class Circle() extends Shape{

public void draw(){

System.out.println(“Draw circle”);

}

}

public class Triangle() extends Shape{

public void draw(){

System.out.println(“Draw triangle”);

}

}

public class Test{

public static void main(String [] args){

Shape s;

s = new Shape();

s.draw();

s= new Circle();

s.draw();

s = new Triangle();

s.draw();

}

}

According to the above program, class Shape has a method draw(). Class Circle and class Triangle extends Shape class. Class Circle and class Triangle can inherit the attributes and methods of class Shape. Therefore, class Shape is the super class or parent class. Class Circle and Class Triangle are sub classes or derived classes. These classes also have draw() method with their own implementations.  Therefore, the draw() method in the super class is overridden.

In the main method, different objects are invoked. There is a reference variable of Shape type, which is s. Then, s invokes the method according to the specific class. At compile time, the compiler will only refer the super class draw method. When actual execution begins, it will lead to the execution of different draw methods. First, s will be pointing to the object of type Shape. Therefore, it will invoke the draw method in Shape class. Then the s will be pointing the object of type Circle, and it will invoke the draw method of Circle class. Finally, s will be referring to the object of type Triangle, and it will invoke the draw method in Triangle class. Even though the reference variable is of type Shape, the binding happens to depend on the object type. This concept is known as Dynamic Binding. The information is provided at run time, so the speed of execution is slower comparing to static binding.

What is the Similarity Between Static Binding and Dynamic Binding?

  • Both these are related to a polymorphism that allows an object to behave in multiple ways.

What is the Difference Between Static Binding and Dynamic Binding?

Static Binding vs Dynamic Binding

Static Binding is the type of binding that collects all required information to call a function during compile time. Dynamic Binding is the type of binding that collects all required information to call a function during run time.
 Time of Binding
Static Binding occurs at compile time. Dynamic binding occurs at run time.
Functionality
Static Binding uses type information for binding. Dynamic Binding uses objects to resolve to bind.
 Actual Object
Static binding does not use an actual object for binding. Dynamic binding, use the actual object for binding.
Synonyms
Static binding is also known as early binding. Dynamic binding is also known as late binding.
Execution
The execution speed is fast in static binding. The execution speed is low in dynamic binding.
Example
Static binding is used in method overloading. Dynamic binding is used in method overriding.

Summary – Static Binding vs Dynamic Binding 

There is a link between a method call and method definition. It is known as binding. There are two types of bindings called static binding and dynamic binding. The difference between static binding and dynamic binding is that in static binding, the binding is resolved at the compile time while dynamic binding is resolved at the run time, which is the actual time of execution. As the required information is provided before run time, static binding is fast in execution comparing to dynamic binding.

Download the PDF of Static Binding vs Dynamic Binding

You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between Static Binding and Dynamic Binding

Reference:

1.Java Interview 04 – Static Binding Vs Dynamic Binding, Mahika Tutorials, 27 Dec. 2017. Available here 

Related posts:

Key Difference Between Overloading and Overriding in JavaDifference Between Overloading and Overriding in Java Difference Between Assembly and DLL Difference Between Java5 and Java6 Difference Between Machine Dependent and Machine Independent Code OptimizationDifference Between Machine Dependent and Machine Independent Code Optimization Difference Between Python and C LanguageDifference Between Python and C Language

Filed Under: Programming Tagged With: Compare Static Binding and Dynamic Binding, Dynamic Binding, Dynamic Binding Definition, Dynamic Binding Execution, Dynamic Binding Function, Dynamic Binding Time, early binding, Late binding, static binding, Static Binding and Dynamic Binding Differences, Static Binding and Dynamic Binding Similarities, Static Binding Definition, Static Binding Execution, Static Binding Function, Static Binding Time, Static Binding vs Dynamic Binding

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Administration and Liquidation

Difference Between Administration and Liquidation

Difference Between Anxiety and Depression

Difference Between Anxiety and Depression

What is the Difference Between Oxazole and Isoxazole

What is the Difference Between Oxazole and Isoxazole

What is the Difference Between Foundation and BB Cream

What is the Difference Between Foundation and BB Cream

Difference Between Adhesion and Cohesion

Difference Between Adhesion and Cohesion

Latest Posts

  • What is the Difference Between Osteoporosis and Scoliosis
  • What is the Difference Between Saree and Half Saree
  • What is the Difference Between Hypernatremia and Hyponatremia
  • What is the Difference Between Diabetes Mellitus and Diabetic Ketoacidosis
  • What is the Difference Between Sciatica and Spinal Stenosis
  • What is the Difference Between Metatarsalgia and Morton’s Neuroma
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.