Polymorphism is nothing but " one method can we use many ways"
We will take one method that method have method name and parameters
1) reuse()//method without- parameters
Now, we are taking same method
2) reuse(int a)//with single parameter
3) reuse(int a, int b)//with double parameters
Above we used one method name "reuse" with created 3(many) different "reuse" methods is nothing but polymorphism
There are two forms of Polymorphism, static and dynamic.
Static polymorphism can be achieved making use of method overloading, meaning several methods with the same name but different parameters. (Check my profile)
Dynamic polymorphism can be achieved by method overriding, so lets take example (In my profile), I have a rectangle[ShapeRectangle] which is a subclass and is assigned to the superclass called Shape [which in this case is a square]. So now if we call the overriden method[ShapeRectangle] on the super class reference [Shape], then it will return the value of the [ShapeRectangle]
Means everyone can dance:
But depend on their countries, cultures, they would dance differently.
Mexican mexBoy = new Mexican();
mexBoy.dance(); //Mexican style
Indian indiGirl = new Indian();
indiGirl.dance(); //Indian style
Polymorphism is the basic Java principle by which one method can be used for more than one purpose. Let's implement polymorphism in the Shapes class in the code below to use the method "area" to find the area of a square, a rectangle, a circle and a triangle.
For more information, see the Sololearn tutorial on Java polymorphism.