Polymorphism
Polymorphism came from the two greek words 'poly' meaning many and 'morphos' meaning forms. The ability to exist in different forms 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. As the same variable or method can perform different tasks, the programmer has the advantage of writing flexible code.
Polymorphism With Variables
Java compiler decides the data type of the result of the expression a+b
depending on the data types of a and b. If a
and b
are int type, then a+b
will also be taken as int type. If a
and b
are float type variables, then a+b
will be taken as float type. If a
is int and b
is float, then the compiler converts a
also into float and then sum is found. Thus, the result a+b
is exhibiting polymorphic nature.
int a = 10;
float b = 12.2;
System.out.println(a+b);
Polymorphism With Methods
If the same method performs different tasks, then the method is said to exhibit polymorphism. Decision of which method is
called in a particular context, is taken at the compile time or run time. The word static
represents at compile time and dynamic
represents at run time.
Dynamic Polymorphism
The polymorphism exhibited at runtime 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. Java method 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 polymorphism
. If both the methods have same name, JVM observes the signature of the methods.
Method Signature
Method signature represents the method name along with method parameters. Even if to methods have same name, their signature may vary for example, two human beings may have same name, but their signatures will differ.
Method Overloading
Writing two or more methods in the same class in such a way that each method has same name, but different method signatures is called method overloading.
Example:
The below two method names are the same, but the signature is different for both.
int sum(int a, int b); // method signature with two parameters
int sum(int a, int b, int c); // method signature with the same name but with three parameters
Method Overriding
Writing two or more methods in super and subclasses such that the methods have same name and same signatures is called method overrding. In method overriding java compiler does not decide which method is called by the user, since it has to wait till an object to subclass is created. After creating the object JVM has to bind the method call to an appropriate method. But the super and subclasses have same name and same method signatures.
// dynamic polymorphism
class One {
// method to calculate square value
void calculate(double x) {
System.out.println("Square value= " + (x * x));
}
}
class Two extends One {
// method to calculate square root value
void calculate(double x) {
System.out.println("Square value= " + Math.sqrt(x));
}
}
class Poly {
public static void main(String args[]) {
// create subclass object t
Two t = new Two();
// call calculate() method using t
t.calculate(25);
}
}
The output is
Square value= 5.0
Static Polymorphism
The polymorphism exhibited at the compilation time is called static polymorphism. Here, the java compiler knows without any ambiguity which method is called at the time of compilation. Of course, JVM executes the method later, but the compiler knows and can bind the method call with method code at the time of compilation. So, it is also called static binding
or compile time polymorphism
.
// static polymorphism
class One {
// method to calculate square value
static void calculate(double x) {
System.out.println("Square value= " + (x * x));
}
}
class Two extends One {
// method to calculate square root value
static void calculate(double x) {
System.out.println("Square value= " + Math.sqrt(x));
}
}
class Poly {
public static void main(String args[]) {
// super class reference refers to subclass objects
One o = new One();
// call calculate() method using super class reference
o.calculate(25);
}
}
The output is
Square value= 625.0