Static Binding and Dynamic Binding

Static Binding and Dynamic Binding

static binding and dynamic binding in java

Connecting a method call to the method body is known as binding.

There are two types of binding

  1. Static Binding (also known as Early Binding).
  2. Dynamic Binding (also known as Late Binding).

Static vs. Dynamic Binding in java

Understanding Type

Let’s understand the type of instance.

1) variables have a type

Each variable has a type, it may be primitive and non-primitive.

Here data variable is a type of int.

2) References have a type

3) Objects have a type

An object is an instance of particular java class,but it is also an instance of its superclass.
Here d1 is an instance of Dog class, but it is also an instance of Animal.

static binding

When type of the object is determined at compiled time(by the compiler), it is known as static binding.

If there is any private, final or static method in a class, there is static binding.

Example of static binding


Dynamic binding

When type of the object is determined at run-time, it is known as dynamic binding.

Example of dynamic binding

Test it Now

Output:dog is eating...
In the above example object type cannot be determined by the compiler, because the instance of Dog is also an instance of Animal.So compiler doesn’t know its type, only its base type.

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263827.html

(0)
上一篇 2022年6月1日
下一篇 2022年6月1日

相关推荐

发表回复

登录后才能评论