Difference between Early and Late Binding in Java


In object-oriented programming, official refers to the method of interfacing a strategy call to its execution. Java, an object-oriented programming dialect, supports early official and late authoritative which are too known as inactive authoritative and energetic authoritative, separately. Both forms of binding have advantages and applications. We will look at the syntax, explanation, and distinctions between early and late binding in Java in this post.

Syntax

The syntax for early binding in Java is as follows.

<ClassName> <objectName> = new <ClassName>();

The syntax for late binding in Java is as follows.

<ClassName> <objectName> = new <DerivedClassName>();

Explanation of Syntax

The class type is decided at compile time in early binding, and the method implementation is chosen depending on the object's specified type. This means that the compiler is aware of the object's specific class and may tie the method call straight to the method implementation.

Late binding, on the other hand, determines the class type at runtime and selects the method implementation depending on the actual type of the object. This indicates that the compiler is unaware of the object's precise class and must rely on the runtime environment to find the right method implementation.

Approach 1: Early Binding

In early binding, the method call is resolved at compile time. Let's consider the following algorithm for early binding −

  • Declare a class called Shape with a method called draw().

  • Create a subclass called Circle that extends the Shape class.

  • Implement the draw() method in the Circle class.

  • Create an object of the Circle class using early binding.

  • Call the draw() method on the object.

Example

class Shape {
   public void draw() {
      System.out.println("Drawing a shape");
   }
}

class Circle extends Shape {
   @Override
   public void draw() {
      System.out.println("Drawing a circle");
   }
}

public class Main {
   public static void main(String[] args) {
      Shape shape = new Circle();
      shape.draw();
   }
}

Output

Drawing a circle

Explanation of the code in approach 1

We have a Shape class with a draw() function that prints "Drawing a shape" in this code. We also have a Circle class that extends the Shape class and overrides the draw() function to output "Drawing a circle". In the Main class, we create an object of the Circle class using early binding by declaring it as a Shape type. When we call the shape object's draw() function, the result will be "Drawing a circle." This is because the method call is tied to the Circle class's implementation at build time.

Approach 2: Late Binding

In late binding, the method call is resolved at runtime. Let's consider the following algorithm for late binding −

  • Declare a class called Animal with a method called makeSound().

  • Create two subclasses called Dog and Cat that extend the Animal class.

  • Implement the makeSound() method in both the Dog and Cat classes.

  • Create an object of the Dog class using late binding.

  • Call the makeSound() method on the object.

Example

class Animal {
   public void makeSound() {
      System.out.println("Animal makes a sound");
   }
}

class Dog extends Animal {
   @Override
   public void makeSound() {
      System.out.println("Dog barks");
   }
}

class Cat extends Animal {
   @Override
   public void makeSound() {
      System.out.println("Cat meows");
   }
}

public class Main {
   public static void main(String[] args) {
      Animal animal = new Dog();
      animal.makeSound();
   }
}

Output

Dog barks

Explanation of the code in approach 2

In this code, we have an Animal class with a makeSound() method that prints "Animal makes a sound". We also have two subclasses, Dog and Cat, that extend the Animal class and override the makeSound() method to print "Dog barks" and "Cat meows", respectively. In the Main class, we create an object of the Dog class using late binding by declaring it as an Animal type. When we call the makeSound() method on the animal object, the output will be "Dog barks". This is because the method call is bound to the implementation of the Dog class at runtime based on the actual type of the object.

Difference between Early and Late Binding in Java

Points of Difference

Early Binding

Late Binding

Resolution Time

Compile time

Runtime

Method Implementation

Determined based on the declared type of the object

Determined based on the actual type of the object

Flexibility

Limited flexibility to change the method implementation dynamically

Provides flexibility through dynamic method dispatch and polymorphism

Performance

Faster performance as method calls are resolved at compile time

Slightly slower performance as method calls are resolved at runtime

Object Declaration

Object declaration uses the class type

Object declaration uses the derived class type

Conclusion

Early binding and late binding are two important concepts in Java that determine how method calls are resolved. While late binding resolves the method call depending on the actual type of the object at runtime, early binding links the method call to its implementation at compile time. Each method has a unique set of advantages and uses. Although early binding offers better performance since the method call is resolved at compile time, it does not allow for dynamic changes to the method implementation. Late binding, on the other hand, allows for dynamic method dispatch, enabling polymorphism and flexibility in method invocation. Understanding the differences between early and late binding is crucial for writing efficient and flexible Java programs.

Updated on: 28-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements