+ 16
Please explain me the Static Binding and Dynamic Binding in Java
I am not able to get the proper explanation for both.
4 Antworten
+ 8
✨The INDIAN✨ Reference is taken from
https://www.javatpoint.com/static-binding-and-dynamic-binding
For a better explanation and understanding, kindly do an online search.
As Hardik Sharma said, suppose a case of static binding -
class A {}
class B extends A {}
public static void main(String args []){
B obj = new B();
}
In the above code, obj is an instance of class B as well as of class A(due to inheritance). The compiler can easily determine the type of obj object. This is called static binding. If there is any private, final or static method in a class, there is static binding.
Dynamic Binding is when type of the object is determined during run time and not by the compiler. For example,
class A {}
class B extends A {}
public static void main (String args []){
A obj = new B();
}
Here the compiler can't determine the type of instance obj. All it knows is it belongs to a Base class A.
+ 6
In simple words,
When type of object is determined at compile time then it is static binding whereas when type of object is determined at runtime then it is dynamic binding.
Thank you
+ 5
✨The INDIAN✨ Also, check out https://www.google.com/amp/s/www.geeksforgeeks.org/static-vs-dynamic-binding-in-java/amp/ for a good read