Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Static Binding and Dynamic Binding In Java

Last Updated on December 22, 2023 by Ankit Kochar

In the world of Java programming, understanding the concepts of static binding and dynamic binding is fundamental to writing efficient and flexible code. Binding refers to the process of connecting a method call to its definition. In Java, two types of binding mechanisms, namely static binding and dynamic binding, play a crucial role in determining how method calls are resolved during the program’s execution. This distinction is essential for developers to design robust and maintainable software solutions. Let’s delve into the intricacies of static and dynamic binding and explore their significance in Java programming.

What is Binding in Java?

Before directly jumping into a static binding and dynamic binding let’s first have a brief idea about binding. In java, binding refers to the process of linking a method call to its corresponding method implementation. Binding decides which method implementation is classed when a method is called. Binding can happen either at runtime or at compile time.

Binding and polymorphism, which refer to the capacity of objects of various classes to be treated as though they are objects of the same class, are closely related in Java. Inheritance and method overriding, which let a subclass offer a distinct implementation of a method that is already specified in its superclass, are two ways that polymorphism is accomplished. Whether static or dynamic binding is used depends on the nature of the reference variable that is used to invoke the function.

Types of Binding in Java

In java, there are mainly two types of binding and they are

  • Static Binding: Happens at compile time
  • Dynamic Binding: Happens at run time

Static Binding

Static binding can also be named early binding. The method call in static binding is resolved at the compile time, which means the method to be executed and the corresponding implementation is determined at the time of compilation. It is performed based on the type of reference variable, rather than the object that it refers to. Mainly it is used when the method is defined in the class and there is no overriding of the method in the subclass.

Static binding is also known as early binding. In static binding, the method call is resolved at compile-time, which means the method to be executed is determined at the time of compilation. This binding is performed based on the type of the reference variable, rather than the object that it refers to. It is usually used when the method is defined in the class and there is no overriding of the method in the subclass.

Example of Static Binding in Java

In this section, we will see an example of static binding with code implementation and example.

class Shape{
  void draw(){
      System.out.println("Drawing Shape");
  }
}
class Rectangle extends Shape{
  void draw(){
      System.out.println("Drawing Rectangle");
  }
  void print(){
      System.out.println("Printing Rectangle");
  }
}
class Test{
  public static void main(String[] args){
      Shape s1 = new Shape();
      Shape s2 = new Rectangle();
      Rectangle r = new Rectangle();
      s1.draw();
      s2.draw();
      r.print();
  }
}

Output

Drawing Shape
Drawing Rectangle
Printing Rectangle

Explanation of the above example
In the above example we have a child class rectangle that extends the parent class shape. The shape class has a draw() method and the rectangle class also has a draw() method but the parent calls prints “Drawing Shape” whereas the rectangle class prints “Drawing Rectangle” to the console and there is also the print() method in the rectangle class that prints “Printing Rectangle” to the console.

Now in the main method, we have created three objects one of type shape and 2 of type rectangle. S1 is of type shape and s2 and r are of type rectangle.
In the main method, we create three objects – s1 of type Shape, s2 of type Rectangle, and r of type Rectangle. The draw() function will display "Drawing Shape" to the console when we use s1 to call it. When we call the draw() function using s2, it will display "Drawing Rectangle" to the console. This is so because the reference variable s2’s class, Shape, determines whether the draw() function is invoked. When we execute the print() function using r, it will print “Printing Rectangle” to the console.

Advantages of Static Binding

The advantages of static binding are given below.

  • As it is resolved at compile time it is faster.
  • It is less prone to errors as during compilation the compiler checks for any error in the method and its implementation.

Disadvantages of Static Binding

The disadvantages of static binding are given below.

  • It is less flexible as it cannot be changed at runtime.
  • Sometimes it leads to unexpected behavior when the reference type does not match the object type.

Dynamic Binding

Dynamic binding is also known as late binding or runtime polymorphism. In dynamic binding, the method call is resolved at runtime, which means the method to be executed is determined at the time of execution. This binding is performed based on the type of object that it refers to, rather than the type of reference variable. It is usually used when the method is overridden in the subclass. It is resolved based on the object type.

Example of Dynamic Binding in Java

Now we will see an example of dynamic binding with proper code and explanation.

class Shape{
  void draw(){
      System.out.println("Drawing Shape");
  }
}
class Rectangle extends Shape{
  void draw(){
      System.out.println("Drawing Rectangle");
  }
  void print(){
      System.out.println("Printing Rectangle");
  }
}
public class Test{
  static void printDrawing(Shape s){
      s.draw();
  }
  public static void main(String[] args){
      Shape s1 = new Shape();
      Shape s2 = new Rectangle();
      Rectangle r = new Rectangle();
      printDrawing(s1);
      printDrawing(s2);
      r.print();
  }
}

Output

Drawing Shape
Drawing Rectangle
Printing Rectangle

Explanation of the above code
In the above example we have a parent class named shape and child class named rectangle. The draw method in the shape class is printing “Drawing Shape” whereas the draw method in the rectangle class is printing” Drawing Rectangle” whereas the print method is printing “ Printing Rectangle” to the console.

We have also created a static method printDrawing() that takes an argument of type Shape. This method calls the draw() method using the Shape reference variable.

In the main method, we create three objects – s1 of type Shape, s2 of type Rectangle, and r of type Rectangle. We call the printDrawing() method using s1 and s2. When we call it using s1, it will print “Drawing Shape” to the console. When we call it using s2, it will print “Drawing Rectangle” to the console. This is because the draw() method is called based on the actual object type, which is Shape for s1 and Rectangle for s2. When we call the print() method using r, it will print “Printing Rectangle” to the console.

Advantages of Dynamic Binding

Below are some of the advantages of dynamic binding.

  • It is more flexible as we can change the object at runtime.
  • It allows for polymorphism, which means the same method can behave differently depending on the object type.

Disadvantages of Dynamic Binding

Some of the disadvantages of dynamic binding are given below.

  • As it resolves at run time so it is comparatively slower than static binding.
  • It is more error-prone as the compiler is not checking for errors at compile time.

Difference between Static Binding and Dynamic Binding

Now we will see some points of difference between static binding and dynamic binding.

Parameter Static Binding Dynamic Binding
Time of Binding Occurs at compile time Occurs at runtime
Method Resolution Method is executed based on the type of reference variable used to call the method Method is executed is based on the actual type of object that the reference variables refer to.
Overloading and OVerriding Static binding is used for method overloading. Dynamic binding is used for method overriding.
Flexibility It is less flexible than dynamic binding It is more flexible than static binding
Performance It is faster than dynamic binding. It is slower than static binding.

Conclusion
In conclusion, a solid understanding of static binding and dynamic binding in Java is essential for Java developers aiming to create efficient, adaptable, and maintainable code. Static binding, with its compile-time resolution, offers performance benefits, while dynamic binding provides flexibility and polymorphic capabilities at runtime. Striking the right balance between these two binding mechanisms is crucial for designing robust and scalable Java applications. Armed with this knowledge, developers can make informed decisions to optimize code execution and build software that meets the demands of diverse and dynamic environments.

Frequently Asked Questions related to Static Binding and Dynamic Binding In Java

Here are some of the frequently asked questions about static binding and dynamic binding.

1. Is polymorphism an example of static binding or dynamic binding?
Polymorphism is an example of dynamic binding because the method implementation is determined at runtime based on the actual object type.

2. What is a virtual method in Java?
A virtual method is a method that is resolved at runtime based on the actual object type.

3. How does Static Binding contribute to performance optimization?
Static binding allows the Java compiler to optimize method calls during the compilation phase. Since the method to be called is known at compile time, the compiler can perform various optimizations, leading to faster and more efficient code execution. However, the downside is that it lacks the flexibility provided by dynamic binding.

4. What advantages does Dynamic Binding offer in Java?
Dynamic binding enhances code flexibility and promotes polymorphism. It enables the creation of more adaptable and extensible systems by allowing method implementations to be determined dynamically at runtime. This is particularly useful in scenarios where the exact method to be called may only be known during program execution.

5. How can a developer achieve Dynamic Binding in Java?
Dynamic binding is achieved through method overriding, a key feature of polymorphism in Java. When a subclass provides a specific implementation for a method that is already present in its superclass, dynamic binding comes into play. The actual method called is determined by the type of object the reference variable is pointing to at runtime.

Leave a Reply

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