Static vs Dynamic Binding in Java (original) (raw)

Last Updated : 7 Mar, 2023

There are certain key points that are needed to be remembered before adhering forward where we will be discussing and implementing static and dynamic bindings in Java later concluding out the differences.

Static Binding

The binding which can be resolved at compile time by the compiler is known as static or early binding. The binding of all the static, private, and final methods is done at compile-time.

Example:

Java `

// Java Program to Illustrate Static Binding

// Main class class NewClass {

// Static nested inner class
// Class 1
public static class superclass {

    // Method of inner class
    static void print()
    {

        // Print statement
        System.out.println(
            "print() in superclass is called");
    }
}

// Static nested inner class
// Class 2
public static class subclass extends superclass {

    // Method of inner class
    static void print()
    {

        // print statement
        System.out.println(
            "print() in subclass is called");
    }
}

// Method of main class
// Main driver method
public static void main(String[] args)
{

    // Creating objects of static inner classes
    // inside main() method
    superclass A = new superclass();
    superclass B = new subclass();

    // Calling method over above objects
    A.print();
    B.print();
}

}

`

Output

print() in superclass is called print() in superclass is called

Output Explanation: As you can see, in both cases the print method of the superclass is called. Let us discuss how this happens

As an exercise, the reader can change the reference of object B to subclass and then check the output.

Dynamic Binding

In Dynamic binding compiler doesn't decide the method to be called. Overriding is a perfect example of dynamic binding. In overriding both parent and child classes have the same method.

Example:

Java `

// Java Program to Illustrate Dynamic Binding

// Main class public class GFG {

// Static nested inner class
// Class 1
public static class superclass {

    // Method of inner class 1
    void print()
    {

        // Print statement
        System.out.println(
            "print in superclass is called");
    }
}

// Static nested inner class
// Class 2
public static class subclass extends superclass {

    // Method of inner class 2
    @Override void print()
    {

        // Print statement
        System.out.println(
            "print in subclass is called");
    }
}

// Method inside main class
public static void main(String[] args)
{

    // Creating object of inner class 1
    // with reference to constructor of super class
    superclass A = new superclass();

    // Creating object of inner class 1
    // with reference to constructor of sub class
    superclass B = new subclass();

    // Calling print() method over above objects
    A.print();
    B.print();
}

}

`

Output

print in superclass is called print in subclass is called

Output Explanation: Here the output differs. But why? Let's break down the code and understand it thoroughly.

Tip: Geeks, now the question arises why binding of static, final, and private methods is always static binding?

Static binding is better performance-wise (no extra overhead is required). The compiler knows that all such methods cannot be overridden and will always be accessed by objects of the local class. Hence compiler doesn’t have any difficulty determining the object of the class (local class for sure). That’s the reason binding for such methods is static.

Let us now finally see the differences between static and dynamic binding that is as follows:

Static Binding Dynamic Binding
It takes place at compile time for which is referred to as early binding It takes place at runtime so do it is referred to as late binding.
It uses overloading more precisely operator overloading method It uses overriding methods.
It takes place using normal functions It takes place using virtual functions
Static or const or private functions use real objects in static binding Real objects use dynamic binding.