What is runtime polymorphism

What is the difference between dynamic and static polymorphism in Java?

Binding refers to the link between method invocation and method definition.

This picture clearly shows what is mandatory.

In this picture, the call "a1.methodOne ()" links to the corresponding definition of methodOne () and the call "a1.methodTwo ()" links to the corresponding definition of methodTwo ().

A correct method definition should be available for each method call. This is a rule in Java. If the compiler does not see the correct method definition for each method call, it will throw an error.

Now comes the static and dynamic binding in Java.

Static binding in Java:

Static binding is a binding that occurs during compilation. It is also known as early binding because binding occurs before a program actually runs

.

The static bond can be shown as in the picture below.

In this picture, "a1" is a class A reference variable that points to a class A object. "A2" is also a reference variable of the class A type, but to a class B object.

During compilation, during binding, the compiler does not check the type of object pointed to by a particular reference variable. It only checks the type of reference variable via which a method is called and checks whether a method definition exists for it in this type.

For example, for the method call "a1.method ()" in the above figure, the compiler checks whether a method definition in class A exists for method (). Because "a1" is of type A. When the method "a2.method ()" is called, it is also checked whether a method definition in class A is available for method (). Since "a2" is also of the class A type. There is no check to which object "a1" and "a2" point. This type of binding is known as static binding.

Dynamic binding in Java:

Dynamic binding is a binding that occurs at run time. It's also called Late Binding because binding occurs when the program is actually .__. To run.

Actual objects are used for binding at runtime. For example, for the call "a1.method ()" in the picture above, method () of the current object to which "a1" points is called. For the call "a2.method ()", method () of the actual object is called, to which "a2" points. This type of binding is known as dynamic binding.

The dynamic binding of the above example can be demonstrated as follows.

Reference static binding and dynamic binding in Java