Polymorphism in Java
What is Polymorphism in Java?
Defining the multiple functions (or) methods with the same name associated with the same object is called polymorphism.
Types of polymorphism: There are two types of polymorphism. They are:
1) Static polymorphism (or) Compile time polymorphism.
2) Dynamic polymorphism (or) Run-time polymorphism.
Static polymorphism (or) Compile time polymorphism:
Defining multiple functions (or) methods with the same name within the same class by changing the data types of the parameters is called as Static polymorphism (or) Compile time polymorphism.
Example using the program:
class StaticPoly
{
void fun1()
{
System.out.println("Fun1() of Static Polymorphism");
}
void fun1(int x)
{
System.out.println("Fun1(int x) of Static Polymorphism");
}
void fun1(int x, int y)
{
System.out.println("Fun1(int x, int y) of Static Polymorphism");
}
public static void main(String[] args)
{
StaticPoly p1 = new StaticPoly();
p1.fun1();
p1.fun1(1);
p1.fun1(1,2);
}
}
|
Output:
Fun1() of Static Polymorphism.
Fun1(int x) of Static Polymorphism.
Fun1(int x, int y) of Static Polymorphism.
|
Q) What happens in static polymorphism at the time of compilation?
A) In static polymorphism out of multiple methods with the same name the method which has to get executed would be decided at the time of compilation itself. So we call the static polymorphism as compile time polymorphism.
Rules-based on which the Polymorphism works in Java:
1) Whenever a function call is made by passing a value has an argument then JVM searches for the method defined to accept the same data type of the value that is passed.
2) There is a method defined to accept the same data type is existing JVM would not be considered any other method whose parameter value matches to the passed argument. The first priority given to the method defined to accept the same data type of the value we pass has an argument.
3) When a function call is made by passing value has an argument & if there is no method is defined to accept the data type value passed then it may reach to the alternate method.
4) The alternate method is a method where parameter value where we passed matches the argument but the data type of the parameter and the type of the value what we pass has argument would not be same.
Example:
class Poly
{
void funA(float f)
{
System.out.println(" F = " +f);
}
void funB(boolean b)
{
System.out.println(" B = " +b);
}
public static void main(String[] args)
{
Poly p1 = new Poly();
p1.funA(10);
}
}
|
Output: F = 10.0
Explanation: In the above program the statement “p1.funA(10)” the value “10” is passed to the funA(). At calling position first the value “10” with an integer is not declared so it passed to the void funA(float f). Where float is an alternate method is acceptable to the Integer value we passed.
Dynamic polymorphism (or) Run-time polymorphism:
Defining functions (or) methods in the subclass with the same name and the same signature of the superclass function are called method overloading (or) Dynamic polymorphism.
Example:
Super class program:
class A
{
int i,j;
void funA()
{
System.out.println("FunA() of A");
}
}
Sub class program:
class D extends A
{
int i,x;
void funA()
{
System.out.println("FunA() of class B");
}
public static void main(String[] args)
{
D b1 = new D();
b1.funA();
}
}
|
Output: FunA() of class B.
Explanation: Whenever we are overriding a superclass method in a sub-class the signature and the prototype of the sub-class method must be the same as the super-class method without which where a compilation error would occur. Whatever the rules permitted in static polymorphism are also applicable in method overriding.
Note: In Dynamic polymorphism, multiple methods with the same name & with the same data types of the parameters have to get executed and would be decided dynamically at the runtime-based conditions.