Introduction
This article will explain one of the concepts of Java, Binding, along with some simple examples related to the concept to make a clear understanding.
Concept of Binding in Java
If you have more than one method of the same name or two variables of the same name then there will be confusion during the compile-time and runtime of which method or variable is to be used as the reference code. This problem can be handled by the binding concept. Actually a connection made between the method call and the method body is known as binding.
Types of Binding
- Static binding (early binding)
- Dynamic binding (late binding)
Before proceeding further let's explain some basic definitions.
We need to understand the type of instances that are used in Java and they are variable, references, and object.
Variable type
int A=30; float B=2.4; char C="Rahul";
Here the three variables A, B, C are of different types.
Reference type
- class Electronics {
- public static void main(String args[]) {
- Electronics phone=new Electronics(); } }
Here phone is a type of electronics. Phone is a reference of electronics.
Object type
- class Building {
- void structure() {
- System.out.println("10 floors");
- }
- }
- class Room extends Building {
- void structure() {
- System.out.println("B H K flat");
- }
- public static void main(String args[]) {
- Building b = new Room();
- b.structure();
- }
- }
Here nokia is an instance of the phone class, but it is also an instance of the electronics class.
Static binding
When the binding can be resolved or the object can be determined at compile time by the compiler, then it is known as static binding. It is also called early binding.
All final, static, and private methods and variables are resolved by the static binding.
All overloaded methods are resolved by the static binding. Static binding uses type (class in Java) information to resolve binding.
A simple example of static binding is as in the following:
- class Flower {}
- class Rose extends Flower {
- void
- function() {
- System.out.println("Rose oil");
- }
- public static void main(String args[]) {
- Rose r = new Rose();
- r.function();
- }
- }
Output
Rose oil
Here we have created an object of the Rose class and called the function() method of the same class. Since there is no confusion here, the compiler is able to resolve the binding at the compile time.
Dynamic binding
When the binding can be resolved or the object can be determined at the runtime, then it is known as dynamic binding. It is also called late binding.
All virtual methods are bound during runtime based upon the runtime object. The dynamic binding uses the object to resolve the binding.
Actually, overridden methods are the best example of dynamic binding because there is an overriding of both the parent and child classes that have the same method. When calling the overridden method, the compiler gets confused between the parent and child class methods since both have the same name.
A simple example of Dynamic binding is as in the following:
- class Building {
- void structure() {
- System.out.println("10 floors");
- }
- }
- class Room extends Building {
- void structure() {
- System.out.println("B H K flat");
- }
- public static void main(String args[]) {
- Building b = new Room();
- b.structure();
- }
- }
Output
B H K flat
Since Room extends Building, Room is a subclass or child class and Building is the superclass or parent class. Both of these classes have the same name as the method, structure(). Since we have assigned the reference of the parent class to the child class object, during the call of the structure() method the compiler gets confused about which structure method to call.