Compile Time Polymorphism in Java | Static Binding in Java

Static Binding in Java

Compile Time Polymorphism in Java or Static Binding in Java


Compile time polymorphism or static binding in java is nothing but an concept in which object knows which method will be invoked at compile time. Method overloading is the part of static binding. In this tutorial, we will discuss about static binding and how it is achieved in java programming.

Static Binding

Static binding actually happens during the compilation of your program. It is sometimes called early binding. In this concept, binding happens before our program actually runs. Means, Object knows which method will be invoked when program will run. 

Let’s take an example to understand it better.

Static Binding in Java Look at the example above. We have created a separate class Bike with two methods runBike() and stopBike() to demonstrate static binding or early binding. Below things are happening here.

  • First we create an object of Bike class and pass the reference to the reference variable of the same type.
  • Now we call both the methods from the object.
What compiler will do now.
  • Compiler will start binding process during compilation.
  • Compiler does not check the type of the object, it just checks the type of the reference variable (type means Class).
  • Compiler find that the type of the reference variable is Bike.
  • Now compiler check do this Class having methods runBike() and stopBike() which is true here.
  • Compiler then binds those methods with the reference variable of Bike class type.
  • At run time, method of Bike class will be executed.
Note*: In early binding, compiler binds the methods which exist in the type of reference variable not in the type of Object. Because type of the object can change at run time but the type of reference variable can never be changed. That’s why in early binding compiler consider the type of reference variable. Check out below example :
class Bike {
  public void runBike() {
      System.out.println("Run parent bike.");
  } 
}

class Bullet extends Bike {
  public void getSpeed() {
       System.out.println("Get speed.");
  }
}

class Binding {
   public static void main(String arg[]) {
 
       Bike b = new Bike();
       b.runBike();

       Bike a = new Bullet();
       a.getSpeed(); // You will get error here. Because Bike class don't have getSpeed()
   }
}
Note*: Parent class can hold the reference of child class. But child class can not hold the reference class. 

Look at the above example.

  • We have two classes here, Bike which is parent class and Bullet which is child class of Bike.
  • We created objects for both of them. One reference variable is pointing to object of type Bike and another reference variable is pointing to the object of type Bullet.
  • Now compiler checks, do the type of the reference variable has method runBike() which is true. Then it will bind it to the reference variable.
  • Now compiler checks, do the type of the reference variable which is Bike has method getSpeed() which is false. Then compiler will throw below error.
The method getSpeed() is undefined for the type Bike

So from above error, it is clear that in early binding or static binding type of the reference variable is considered.

2 thoughts on “Compile Time Polymorphism in Java | Static Binding in Java”

Leave a Reply

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