Binding in Java

by Jasleen Chhabra | Updated on 24 August 2024

Table of Contents (Click any Topic to view first)

  1. Static Binding (Early Binding)
  2. Dynamic Binding (Late Binding)
  3. Understanding Type in Java

Binding in Java refers to the process of associating a method call with the method body. There are two types of binding:

  1. Static Binding (Early Binding)
  2. Dynamic Binding (Late Binding)

Static Binding (Early Binding)

Static binding occurs when the type of the object is determined at compile-time by the compiler. This type of binding is used for:

  • Private methods
  • Final methods
  • Static methods

Since these methods cannot be overridden, the method call is resolved at compile-time.

Example:

class Dog {

    private void eat() {

        System.out.println("dog is eating...");

    }

 

    public static void main(String[] args) {

        Dog d1 = new Dog();

        d1.eat();

    }

}

Explanation: In the above example, the eat method is private, so the compiler knows exactly which method to call at compile-time, resulting in static binding.

Dynamic Binding (Late Binding)

Dynamic binding occurs when the type of the object is determined at runtime. This happens when the method is overridden in a subclass.

Example:

class Animal {

    void eat() {

        System.out.println("animal is eating...");

    }

}

 

class Dog extends Animal {

    void eat() {

        System.out.println("dog is eating...");

    }

 

    public static void main(String[] args) {

        Animal a = new Dog();

        a.eat();

    }

}

Explanation: In the above example, the eat method is overridden in the Dog class. The reference variable a is of type Animal, but it holds an instance of Dog. At runtime, the JVM determines the actual object type and calls the eat method of the Dog class, resulting in dynamic binding.

Understanding Type in Java

  1. Variables Have a Type:

int data = 30; // Here, data is of type int

  1. References Have a Type:

class Dog {

    public static void main(String[] args) {

        Dog d1; // Here, d1 is a reference of type Dog

    }

}

  1. Objects Have a Type: An object is an instance of a particular class and can also be considered an instance of its superclass.

class Animal {}

 

class Dog extends Animal {

    public static void main(String[] args) {

        Dog d1 = new Dog();

        // d1 is an instance of Dog, which is also an instance of Animal

    }

}

Summary

  • Static Binding: Determined at compile-time. Used for private, final, and static methods.
  • Dynamic Binding: Determined at runtime. Used for overridden methods.

Binding plays a crucial role in polymorphism, where dynamic binding allows Java to achieve runtime polymorphism, enabling flexible and reusable code.



FAQ

Any Questions?
Look Here.

Related Articles

Abstraction in Java

Break Statement in Java

Classes & Objects in Java

Collections Framework in Java

Comments in Java

Continue Statement in Java

Control Statements in Java

Data Types in Java

Do While-Loop in Java

Encapsulation in Java

Exception Handling in Java

For-Loop In Java

Hello World Program in Java

If-else Statement in Java

Inheritance in Java

Introduction to Java

Java Database Connectivity (JDBC)

Java Development Tools and Frameworks

Java GUI (Graphical User Interface) Programming

Java I/O

Java Vs. C++

Methods and Constructors in Java

Multithreading in Java

Object Oriented Programming in Java

Operators in Java

Polymorphism in Java

Scanner Class in Java

Setting Up Java Environment

Static Keyword in Java

Super Keyword in Java

Switch Statement in Java

This Keyword in Java

Variables in Java

While-Loop in Java