Polymorphism in java :
1. It means one name many forms so we can say that in this type of programming same function is used to perform different kinds of operations.
2. It is an important part of object-oriented programming language.
Example:
Let's take a simple example in which as you can see the class name is poly and there is four-function with the same name a() with different parameters so the execution of the function is based on the value passing at the time of calling.
1: class Poly 2: { 3: void a() 4: { 5: System.out.println("No para"); 6: } 7: void a(int i) 8: { 9: System.out.println("Integer para"); 10: } 11: void a(char ch) 12: { 13: System.out.println("Character para"); 14: } 15: void a(float f) 16: { 17: System.out.println("Float para"); 18: } 19: void a(double d) 20: { 21: System.out.println("Double para"); 22: } 23: void a(String s) 24: { 25: System.out.println("String para"); 26: } 27: } 28: class Easy 29: { 30: public static void main(String[] args) 31: { 32: /*Creating instance(object)*/ 33: Poly obj=new Poly(); 34: obj.a(12); 35: obj.a('e'); 36: obj.a("Easy"); 37: } 38: } 39: /* 40: ### Output ### 41: Integer para 42: Character para 43: String para 44: */
Here the function is called three times first time with integer value and the second time with character value and the third time with string value so the output is
Integer para
character para
String para
Types of Polymorphism:
- Function overloading
- Function Overriding
1: class Geometry 2: { 3: void area(int height,int width) 4: { 5: int ar=height*width; 6: System.out.println("Area of Rectangle="+ar); 7: } 8: void area(int side) 9: { 10: int ar=side*side; 11: System.out.println("Area of Square="+ar); 12: } 13: void area(float r) 14: { 15: float ar=3.14f*r*r; 16: System.out.println("Area of Circle="+ar); 17: } 18: void area(float base,float height) 19: { 20: float ar=0.5f*base*height; 21: System.out.println("Area of Triangle="+ar); 22: } 23: } 24: class Easy 25: { 26: public static void main(String[] args) 27: { 28: /*Creating instance(object)*/ 29: Geometry obj=new Geometry(); 30: /*single para for square*/ 31: obj.area(12); 32: /*double int para for rectangle*/ 33: obj.area(5,6); 34: /*single float for circle*/ 35: obj.area(2.2f); 36: /*double float for trianlge*/ 37: obj.area(2.5f,6.3f); 38: } 39: } 40: /* 41: ### Output ### 42: Area of Square=144 43: Area of Rectangle=30 44: Area of Circle=15.197601 45: Area of Triangle=7.875 46: */
Function Overriding:
1. Function with the same name and the same parameter is called function overriding.
2. It is not possible to make two functions with the same name and same parameter in a single class therefore to implement a function overriding derived class is used.
1: class Geometry1
2: {
3: void area(int height,int width)
4: {
5: int ar=height*width;
6: System.out.println("Area of Rectangle1="+ar);
7: }
8: }
9: class Geometry2 extends Geometry1
10: {
11: void area(int height,int width)
12: {
13: int ar=height*width;
14: System.out.println("Area of Rectangle2="+ar);
15: }
16: }
17: class Easy
18: {
19: public static void main(String[] args)
20: {
21: /*Creating instance(object)*/
22: Geometry2 obj=new Geometry2();
23: /*calling function*/
24: obj.area(12,13);
25: }
26: }
27: /*
28: ### Output ###
29: Area of Rectangle2=156
30: */