Dynamic binding in Java, also known as late binding or runtime polymorphism, refers to the process of determining the functionality of a method call at runtime by the Java Virtual Machine. In other words, the body of a method is bonded to a method call at runtime, and the functionality of the method call is not decided at compile-time. Overriding a method is a perfect example of dynamic binding in Java.
In contrast, static binding, also known as early binding, occurs when the type of the object is determined at compile-time. If there is any private, final, or static method in a class, there is static binding.
To summarize, the key differences between static and dynamic binding in Java are:
- Static binding uses type information for binding, while dynamic binding uses objects to resolve to bind.
- Static binding occurs at compile-time, while dynamic binding occurs at runtime.
- Static binding is used for private, final, and static members (methods and variables), while dynamic binding is used for virtual methods.