What is Polymorphism in Java with example.

What is Polymorphism in Java with example.


Polymorphism is one of the most important OOPS concept that means "one name having many forms".

Polymorphism is Greek word which means "Many Forms", If we break the word, we will get,
Polymorphism = (Poly + Morphism) = Many + Forms.

 Lets understand above line in more details with example.
Polymorphism

Every OOPS concept in general also exist in Real Life, so for better understanding, lets first understand real life example of Polymorphism and later we will see programatic example.

Real Life example of Polymorphism

There are some English words when used without context will have multiple meanings.
Example:

If I don't give you context and simply say word, you will not able to exactly tell what it is used for.
  1. bear:
    • bear can be used to represent a mammal "The black bear". 
    • bear can also be used in context of pain like "I cannot bear his constant criticism".
Similarly,
  1. close:
    • close can be used in context of opposite of far like "My house is close to a bus stop".
    • close can also be used in context of opposite of open like "Please close the door".
So many words in english has multiple meanings that is "same word but multiple meanings".
Isn't it matching with our Polymorphism definition "same name many forms"!!!
Above words when used alone will only give you a broad idea but doesn't signify exact meaning, but same word when used with context will give the exact meaning of the word.
Polymorphism in Java

Similarly in Java, there may be situation where you need to write multiple methods with same name (because they all do same job) but they behave differently based on the parameters supplied.

Example:
  1. getArea(int radius) { return 3.14 * radius * radius; } used to calculate area of Circle.
  2. getArea(int length, int breadth) { return length * bredth; } used to calculate area of Rectangle.

If you look at above two method names "getArea", they both are doing same job of calculating area, so given only name, you cannot say, whether method is used to calculate area of circle, rectangle, triangle, pentagon etc. but you will definitely get a broad idea that method is used to calculate the area.

Parameters supplied to the method will give the exact meaning to the method. whether it is used to calculate area of circle, rectangle etc.
Isn't it is similar to our English words, where full sentence gives exact meaning to the word and in java methods, parameters supplied give exact meaning to the method.
Method with same name is used to calculate the area of Circle.
Method with same name is used to calculate the area of Rectangle and so on,
Method with same name exhibits different behaviour based on parameter list supplied.
I hope you now get fair understanding of "Same Name Multiple Behaviour/Forms."

Remember, Polymorphism is the just the ability of a method to behave differently at different places.
Method with same name can behave differently either by,

  1. Method Overloading
  2. Method Overriding.
 Example used above to explain Polymorphism is actually a Static Polymorphism.

Another example of Polymorphism,

You might have used "System.out.println()" method and passed int, float, String, Object etc as a parameter like,
  1. System.out.println(10) -> 10
  2. System.out.println("hello") -> hello
  3. System.out.println(50.5) -> 50.5
same method name println(), can sometime print integer, sometime float, sometime String etc.
same method name println() has many forms.

Internally there are multiple println() method written as shown below,

If you look at above println methods, they all are doing same job of printing data, 
So if you are given only name, you cannot say, whether method is used to print integer, float, boolean, String etc. but you will definitely come to know that method is used to print data to standard output device.

Parameters supplied to the println method will give the exact meaning to the method. whether it is used to print integer, float, String, boolean etc.

Name of the metod is same "println", but it used to print integer, float, double, object etc.
Same method name but so many behaviours this is what Polymorphism is all about.
  
Conclusion

Method having same name but behave differently (many forms) is called Polymorphism.

Types of Polymorphism

  1. Static Polymorphism - Static Binding - Compile time Polymorphism - Method Overloading
  2. Dynamic Polymorphism - Dynamic Binding - Run time Polymorphism - Method Overriding 
Note:  
Polymorphism is the just the ability of a method to behave differently at different situation, and this is actually achieved either by Method Overloading or by Method Overriding.
We will look into above 2 types of Polymorphism in separate post.

You may also like to see


When to use interface and abstract class in Java?

Real time example of abstract class and interface in Java project.

Java Multithreading and Concurrency Interview Questions and Answers with Example

How ConcurrentHashMap works and ConcurrentHashMap interview questions.

Method Overriding Rules In Java

How is ambiguous overloaded method call resolved in java?

Method overriding and Method hiding Interview Question in Java

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

Post a Comment