Java Static Binding and Dynamic Binding - The Coding Shala

Home >> Learn Java >> Java Static Binding and Dynamic Binding

Java Static Binding and Dynamic Binding

What do you mean by Binding? Binding means connecting a method call to the method body. Binding determines the type of object. There are two types of binding available in Java:
  • Static Binding
  • Dynamic Binding
Java Static Binding and Dynamic Binding - The Coding Shala

Java Static Binding

If the type of object is determined at the compiled time by the compiler, it is known as Static Binding or Early Binding. Binding of all the static, private and final methods is done at compile time or called static binding. We know static, private and final methods can not be overridden and always accessed by an object of a local class. That's why the compiler can easily determine the object of class and binding is done at compile time.

Example of Java Static Binding

The following example explains Java Static Binding: 


//Java Static Biniding Example
//thecodingshala.com 

class Parent{
    static void Display(){
        System.out.println("This is parent class");
    }
    
    final void M1(){
        System.out.println("M1 method of Parent class");
    }
}

class Child extends Parent{
    static void Display(){
        System.out.println("This is Child class");
    }
}

class Main{
    public static void main(String[] args){
        Parent p1 = new Parent();
        p1.Display(); //static binding 
        p1.M1(); //static binding
        Parent p2 = new Child();
        p2.Display(); //final cannot be overridden
    }
}
Output:

This is parent class
M1 method of Parent class
This is parent class

Java Dynamic Binding

When the type of the object is determined at run-time, it is known as dynamic binding. Overriding is an example of dynamic binding. Dynamic binding is also known as Late Binding. The compiler goes only by referencing variable not by the type of object and therefore dynamic binding happens at the run time. 

Example of Java Dynamic Binding

The following example explains the Java Dynamic Binding: 
//Java Dynamic Binding Example
//thecodingshala.com 

class Parent{
    void Print(){
        System.out.println("This is parent class");
    }
}

class Child extends Parent{
    @Override
    void Print(){
        System.out.println("This is child class");
    }
}

class Main{
    public static void main(String[] args){
        Parent p1 = new Parent();
        p1.Print();
        Parent p2 = new Child(); //dynamic binding 
        p2.Print();
    }
}
Output:

This is parent class
This is child class

NOTE: Virtual methods binding is done during run time.

Java Virtual Function

In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature.

NOTE: Java does not have a virtual keyword.

All the non-static are considered as virtual by default. To remove the default virtual feature in each function we have to make it private or final in java classes.

Question: Why the static method is not a virtual method in Java?
Answer: We can override the static method but it will not give the advantage of polymorphism. It is called method hiding not method overriding so static methods are not the virtual method.



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Java Program to Convert Binary to Decimal - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Program to Convert Decimal to Binary - The Coding Shala