Polymorphism
Basically, 'Poly' means many and 'Morph' means behavior or forms so many behaviors in programming. same functionality are same object which has many behaviors.
Example Code for Ploymorphism Concept
There are two common ways of implementing Polymorphism.
1. Overloading
2. Overriding
Overloading
Using the same method name with different parameter types lists.
Overriding
Using the different implementations of the same method in sub classes.
Basically, 'Poly' means many and 'Morph' means behavior or forms so many behaviors in programming. same functionality are same object which has many behaviors.
Example Code for Ploymorphism Concept
class DemoOverload{
public int add(int x, int y){ //method 1
return x+y;
}
public int add(int x, int y, int z){ //method 2
return x+y+z;
}
public int add(double x, int y){ //method 3
return (int)x+y;
}
public int add(int x, double y){ //method 4
return x+(int)y;
}
}
class Test{
public static void main(String[] args){
DemoOverload demo=new DemoOverload();
System.out.println(demo.add(2,3)); //method 1 called
System.out.println(demo.add(2,3,4)); //method 2 called
System.out.println(demo.add(2,3.4)); //method 4 called
System.out.println(demo.add(2.5,3)); //method 3 called
}
}
There are two common ways of implementing Polymorphism.
1. Overloading
2. Overriding
Overloading
Using the same method name with different parameter types lists.
Overriding
Using the different implementations of the same method in sub classes.