What is static and dynamic binding Explain with example how
Solution
First we need to know about the definition of polymorphism.
Polymorphism: If the same method perform different tasks it is called polymorphism.
How can a same method perform different tasks?
The same method performs different tasks only if it is having different bodies.
Here we have two types of polymorphism
1.Dynamic polymorphism or dynamic binding:
Dynamic polymorphism is the polymorphism exhibited at runtime.You mentioned this as late binding.
Here Jvm knows which method is called by the user at runtime. A method call is linked with the method body at the time of running the program. So it is also called as dynamic binding or runtime polymorphism.
Method overloading using instance methods is an example for dynamic polymorphism or dynamic binding
Ie.Method over loading: Writing two or more methods with the same name but with different method signatures is call as method overloading.
Jvm will observe these method signatures difference.
Ex: add(int a, int b)—this method is having two parameters of type int.
add(int a,int b,int c)—this method is having 3 parameters of type int.
When two methods are overloaded jvm will decide which method to be execute depending upon the following factors:
1)There is a difference in the no of parameters.
Ex: void add(int a,int b)
void(int a,int b,int c)
2)The difference in the datatype of the parameters.
Ex: void add(int a,float b)
void add(double a,double b)
3)The difference in the sequence of the parameters
Ex: int swap(int a ,char b)
Int swap(char a,int b)
Any one of the above differences will makemethod signature to be different because,the method signature is different jvm can recognize as different method.
Method overriding using instance methods is an example of dynamic binding.
Method overriding: Writing two or more methods with the same name and same signature is called method overriding.
In method overriding jvm will decide which method is to be executed depending upon the class of the object.Since we create an object to subclass in inheritance jvm executes on subclass method.
In other words sub class method overriding the super class method.
Example program:
public class Sample
{
void add(int a,int b)
{
System.out.println(“Sum of two nos=”+(a+b));
}
void add(int a,int b,int c)
{
System.out.println(“Sum of three nos=”+(a+b+c));
}
}
Class SampleTest
{
public static void main(String args[])
{
Sample s=new Sample();
s.add(10,15);
}
}
Explanation:here in this program,
void add(int a,int b) has parameters of type int and
void add(int a,int b,int c) has three parameters of type int
but s.add(10,15); has two values of type int .So it chooses void add(int a,int b) at runtime.The void add(int a,int b) method gets executed and prints the output sum=25.This means method choosen is done by jvm during runtime.
Lets take another example for dynamic binding:
First we need to know about method overriding.
Method overriding: Writing two or more methods with the same name and same signature is called method overriding.
public class One
{
void calculate(double x)
{
System.out.println(“Square=”+(x*x));
}
}
public classTwo extends One
{
void calculate(douible x)
{
System.out.println(“Square root=”+Math.sqrt(x));
}
}
Class Test
{
public static void main(Strings args[])
{
Two t=new Two();
t.calculate(16);
}
}
Then we will get the output: square root : 4.0
Here in this program the object was created to the class Two(i.e sub class).That’s why JVM has choosen the class Two method for execution at runtime.Here sub class method is overriding the super class.In method overriding JVM will decide which method is to be executed depending upon the class of the object.
Since we created object to the sub class in inheritance,JVm executes only sub class method.
So method overriding using instance method is also an example for dynamic binding.
_____________________
Static Binidng:
Polymorphism is exhibited at compile time is called static binding or static polymorphism.Here the java compiler knows which method is called by the user at compile time.
The method call Is linked with the method body at compilation time.So it is called as static binding or compile time polymorphism.
Achiving method overloading and method overriding using static methods (or ) private methods (or) final methods is an examples for static polymorphism
___________Thank You


