Difference Between Early and Late Binding

Table of Contents

Key Difference – Early vs Late Binding
 

Early Binding and Late Binding are two concepts related to Polymorphism. The Early Binding occurs at compile time while the Late Binding occurs at runtime. The key difference between Early and Late Binding is that Early Binding uses the class information to resolve method calling while Late Binding uses the object to resolve method calling.

Programming languages such as Java supports Object Oriented Programming (OOP). It is a paradigm that allows constructing the program or the software using objects. There are multiple objects in the software. These objects are connected to each other and pass messages using methods. Every object has characteristics and behaviours. The characteristics are described by the properties or attributes. The behaviours are described using methods. The object Student can have characteristics such as name, age and they are represented by properties. The object Student can have behaviours such as study and read, and they are represented by methods. One major pillar of OOP is Polymorphism. It allows an object to behave in multiple ways. Early Binding and Late Binding are two concepts in Polymorphism. Overloading methods are bonded using early binding. Overridden methods are bonded using late binding.

CONTENTS

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

What is Early Binding?

In Early Binding, the class information is used to resolve method calling. Early Binding occurs at compile time. It is also known as the static binding. In this process, the binding occurs before the program actually runs. Overloading methods are bonded using early binding. Refer the below program.

Difference Between Early and Late Binding

Figure 01: Calculation Class

Difference Between Early and Late Binding_Figure 02

Figure 02: Main Program for Early Binding

According to the above program, the Calculation class contains an add method that accepts two integer values and another add method that accepts two double values. In the main program, an object of type Calculation is created. When passing two integers to the add method, it will invoke the add method that accepts two integers. When passing two double values to the add method, it will invoke the method corresponding to two double values. This binding process occurs at compile time. All required information is known before runtime, so it increases the program efficiency and execution speed.

What is Late Binding?

In Late Binding, the object is used to resolve method calling. Late Binding occurs at runtime. It is also known as dynamic binding. In this process, the binding occurs at program execution. Overridden methods are bonded using late binding. Refer the below program.

Difference Between Early and Late Binding_Figure 03

Figure 03: Shape Class

Difference Between Early and Late Binding_Figure 04

Figure 04: Circle Class

Difference Between Early and Late Binding_Figure 05

Figure 05: Triangle Class

Key Difference Between Early and Late Binding

Figure 06: Main program for Late Binding

According to the above program, class Shape has a draw method. Class Circle and class Triangle class extends the Shape class. Therefore, these two classes can inherit the attributes and methods of the Shape class. Shape Class is the base class. Circle and Triangle classes are derived classes. The class Circle and class Triangle also have the draw method with their own implementations. So, the draw method in Shape class is overridden by the draw methods of the derived classes.

In the main program, a reference variable s of type Shape is created. At compile time, the compiler will only refer the base class draw method. At runtime, different draw methods will execute. First, s will point to the object of type Shape. So, the draw method of Shape class is invoked. Then the s will point to the object of type Circle, and it will invoke the draw method of Circle class. Finally, s will point to the object of type Triangle, and it will invoke the draw method in Triangle class. The methods are called depending on the objects. Therefore, the object is used to resolve method calling in Late Binding. The information required for binding is provided at run time, so the speed of execution is slower comparing to early binding.

What is the Similarity Between Early Binding and Late Binding?

  • Both Early Binding and Late Binding are related to polymorphism that is a pillar of OOP.

What is the Difference Between Early Binding and Late Binding?

Early Binding vs Late Binding

The process of using the class information to resolve method calling that occurs at compile time is called Early Binding.The process of using the object to resolve method calling that occurs at run time is called the Late Binding.
 Time of Binding
Early Binding happens at compile time.Late Binding happens at run time.
Functionality
Early Binding uses the class information to resolve method calling.Late Binding uses the object to resolve method calling.
 Synonyms
Early Binding is also known as static binding..Late Binding is also known as dynamic binding.
Occurrence
Overloading methods are bonded using early binding.Overridden methods are bonded using late binding.
Execution Speed
Execution speed is faster in early binding.Execution speed is lower in late binding.

Summary – Early vs Late Binding

OOP is used commonly for software development. One major pillar of OOP is polymorphism. Early Binding and Late Binding are related to that. Early Binding occurs at compile time while Late Binding occurs at runtime. In method overloading, the bonding happens using the early binding. In method overriding, the bonding happens using the late binding. The difference between Early and Late Binding is that Early Binding uses the class information to resolve method calling while Late Binding uses the object to resolve method calling.

Reference:

1.Java Interview 04 – Static Binding Vs Dynamic Binding, Mahika Tutorials, 27 Dec. 2017. Available here
2.Dynamic Method Dispatch in Java | Core Java Tutorial | Studytonight. Available here  

ncG1vNJzZmivp6x7pbXFn5yrnZ6YsqOx07CcnqZemLyue8OinZ%2Bdopq7pLGMm5ytr5Wau26xwKujsmWRo7FuwtJmo5qslWKvqrrDoqWgZw%3D%3D