Articles

What is polymorphism?

What is polymorphism?


Polymorphism is derived from Greek words: 'Poly' which means many and 'Morph' which means shape or form

 

It implies, polymorphism is a feature in java which allows it to invoke or use a method, having same name, for different purposes.

Polymorphism also increases the readability or comprehensibility of the program.

 

For example, say we are writing an application in java that would help anyone find area of different shapes like circle, triangle, rectangle, square and so on. 

In that case, we may have a single method named "area" which would accept different parameters to calculate the area of respective shapes.

 

See below code snippet:

class AreaAPI{ 
   public int area(int side){ //Calculate Square area 
     int squareArea = side*side; 
     return squareArea; 
   } 

   public int area(int l, int b){ //Calculate rectangle area 
   int rectangleArea = l*b; 
   return rectangleArea; 
   } 
}

Now when any other class will try to call the the area method, it will pass required parameters and based on the number of parameters provided in calling method, a respective area method from AreaAPI class will be called, which would calculate the area and return it back. 

 

The above example represents Method Overloading.

As shown in the above example, other programming language (C++, C#, etc.) also use polymorphism in same way with difference in their programming syntax.

 

Every objects in java that has IS-A relationship is polymorphic.


How is polymorphism acheived in OOP?

We can acheive polymorphism in OOP supported language like java in following two ways:

  • Compile time polymorphism or method overloading
  • Run time polymorphism or Dynamic method dispatch or method overriding

Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sonu Pandit
Technology geek, loves to write and share knowledge with the world. Having 10+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Jun 28,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!