Last Updated on March 20, 2023 by Prepbytes
Java is one of the most popular object-oriented programming languages in the world and it has many uses varying from developing software to applications. Binding in java is an important concept as we have to deal with lots of methods and their implementations. Java supports both static binding and dynamic binding. While proceeding further in this article we will learn all about static binding and dynamic binding with their examples, advantages, and disadvantages.
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
Static binding and dynamic binding are important concepts in Java that determine which method will be executed based on the type of the reference variable or the actual object type. In Java, static binding happens at compile time, and Dynamic binding, on the other hand, happens at runtime. We have also seen both static binding and dynamic binding with the help of examples and also have a look at the advantages and disadvantages of static binding and dynamic binding.
Frequently Asked Questions
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. Are all non-static methods in Java virtual methods?
All non-static methods in Java are virtual methods.
4. Can a default constructor be called explicitly in Java?
A default constructor can be called explicitly in Java using the this() method.
5. Which is better among static binding and dynamic binding?
There is no one answer to this question, as it depends on the specific use case. Static binding is generally faster and more efficient, but dynamic binding allows for more flexibility and extensibility in the code. It is up to the programmer to determine which method is best for their specific situation.