How static binding and dynamic binding in java work

Binding is the connection of a method call to the method body. There are two types of binding: Static Binding or early binding and Dynamic Binding or late biding.

Before going into the discussion on Binding, we need to clarify what is Type:

1. Variable has one type, it can be either root or another (not the original).
static-binding-and-dynamic-binding-in-java
Here, the data variable is an int type.

2. References have a style
static-binding-and-dynamic-binding-in-java
3.Objects have one type. The object is an instance of a particular Java class, but it is also an instance of the superclass.
static-binding-and-dynamic-binding-in-java
Here, d1 is a representation of the Dog class, but it is also an expression of Animal.

How static Binding in Java work

When the object type is decided at compile time (by the compiler), it is static binding. If there are any private, final or static methods in a class, it is static binding. Therefore, there can be no overloading of the results for object-oriented programming in static binding

Example of Static Binding
static-binding-and-dynamic-binding-in-java

How dynamic Binding in Java work

When the object type is decided at runtime, it is dynamic binding.

Example of Dynamic Binding
static-binding-and-dynamic-binding-in-java
In the above example, the object type can not be determined by the compiler, because the Dog expression is also an Animal expression. So the compiler does not know what kind of it, just know the base type.

To find out more about how to build constructor in java with detail example:

Comments