In previous posts we have discussed many important topics of object oriented programming like classes, objects, inheritance etc. In this post we will discuss polymorphism in Java which is another important OOP concept.
What is Polymorphism in Java?
The word polymorphism means ‘a state of having many shapes’. In terms of object oriented programming it’s the ability to process objects of various types and classes through a single uniform interface. An object, variable or function may take on multiple forms / shapes and this phenomenon is known as Polymorphism.
Example of Polymorphism in JAVA:
[js]
public int add(int a, int b){
return a+b;
}
public int add(int a, int b, int c){
return a+b+c;
}
[/js]
In above example we can see that the add function have 2 shapes/forms with varying number of input arguments. This is an example of Polymorphism.
Types of Polymorphism in JAVA:
There are two types of Polymorphism in JAVA namely static polymorphism and dynamic polymorphism.
Static Polymorphism in JAVA:
Static polymorphism is achieved through method overloading and is also known as compile time polymorphism or static binding. At compile time JAVA decides which method to call by checking the signature/prototype of the method. Following code example will make the concept clear.
[js]
//file name is Calculate.java
Class Calculate{
public int addNumbers(int a, int b){ //method 1
return a+b;
}
public int addNumbers(int a, int b, int c){ //method 2
return a+b+c;
}
public int addNumbers(double a, int b){ // method 3
return (int)x+y;
}
}
//main class Main.java
Class Main{
public static void main(String[] args){
Calculate c1 = new Calculate();
System.out.println(c1.addNumbers(4+5)); // method 1 will be called
System.out.println(c1.addNumbers(4.5+16)); // method 3 will be called
System.out.println(c1.addNumbers(3+6+12)); // method 2 will be called
}
}
[/js]
Dynamic Polymorphism in JAVA:
Dynamic polymorphism is achieved through method overriding and is also known as dynamic binding or late binding. Let’s say parent class (Human) has a method named as run() and child class (Student) overrides that method run(). Now considering the class reference Java decides which method to call. Following example will elaborate the concept.
[js]
//Human class: Human.java
Class Human{
public void run(){
System.out.println("Human is running");
}
}
//Student class: Student.java
Class Student extends Human{
public void run(){
System.out.println("Student is running");
}
}
//Main class: Main.java
Class Main{
public static void main(String[] args){
Human h1 = new Human();
h1.run(); // prints ‘Human is running’
h1 = new Student(); // polymorphism
h1.run(); // prints ‘Student is running’
}
}
[/js]
In above example h1 is an object of Human class so when we called the run() method Java will call the run() method of Human class. Then we changed the h1 object’s reference to Student class (new shape/form which is polymorphism). Now Java will call the run() method of Student class because now the object’s reference is changed to Student class.
polymorphism is about two or more than two functions having same name and different data type and different sequence ,is this true sir???and what is the benefit of poly…?
Code reuse-ability, having multiple data type variables/functions/objects with same name but different parameters are some of the advantages of polymorphism.
can we use “float” instead of double ,i have use this but there is an error because of float ,why?
Because by default the decimal number type is double in java so at this point
System.out.println(c1.addNumber(4.5+16));
java gonna consider 21.12 values is a double not float hence java look-up table try to find the best match with double value
The float literals as marked with either f or F so to call function with floating values do this
System.out.println(c1.addNumbers(4.5f+16)); // method 3 will be called
Can we say that the concept method overloading and method overriding is called polymorphism?
Yes, method overloading is static polymorphism and method overriding is dynamic polymorphism.
Thank uh Sir!