è
The ability to exist in different form is called Polymorphism
è
In java , a variable, an object or a method can exist in different
forms, thus performing various tasks depending on the context.
è
If the same method
performs different tasks, then that method is said to exhibit polymorphism.
There are
two types of Polymorphism:
1.
Static polymorphism
----
The polymorphism exhibited at compile time is called static
polymorphism.
2.
Dynamic polymorphism----
à The polymorphism exhibited at run time is called dynamic
polymorphism.
è
This means when a method
is called, the method call is bound to the method body at the time of running
the program, dynamically
è
In this case, java compiler does not
know which method is called at the time of compilation, only JVM knows at runtime which method is to be executed.
è
Hence this is also
called runtime polymorphism or dynamic binding.
Example of polymorphism
//dynamic polymorphism
class Sum
{
void add(int a, int b)
{
System.out.println("Sum of
two="+(a+b));
}
void add(int a, int b,int c)
{
System.out.println("Sum of
three="+(a+b+c));
}
}
class Polymorphism
{
public static void main(String
args[])
{
Sum
s=new Sum();
s.add(10,15);
s.add(10,20,30);
}
}
Polymorphism in java
ReplyDeleteGreat keep sharing this is helpful for me