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.
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.
Concept of Binding in Java
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.
int A=30; float B=2.4; char C="Rahul";
Here the three variables A, B, C are of different types.
Here phone is a type of electronics. Phone is a reference of electronics.
Here nokia is an instance of the phone class, but it is also an instance of the electronics class.
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:
Variable type
Reference type
- class Electronics {
- public static void main(String args[]) {
- Electronics phone=new 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();
- }
- }
Static binding
- 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
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.

Gowtham RajamanickamPosted May 19, 2015, 12:50 AM
good one