Polymorphism is a ability to exits in different forms or take more than one form. For example an operation may exhibit different behavior in different instance. The behavior depends upon type of data used in operation. For example addition of two numbers will produce sum ,but if both operands are string then it will produce third string.
For Example in below figure, single method name can be used to handle different number of arguments and different type of arguments.
| Polymorphism |
Object obj = new Object();
Object obj = new String();
Object obj = new Integer(5);
Output :
String Object Found
Undefined Type Object
Polymorphism exists in two forms:
In below example show() method can accept any type of data ,and it will produce output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class PolymorphismExample { public static void show(Object obj){ if(obj instanceof java.lang.String){ System.out.println("String Object Found"); } else if(obj instanceof java.lang.Integer){ System.out.println("Integer Object Found"); } else{ System.out.println("Undefined Type Object"); } } public static void main(String[] args) { PolymorphismExample.show("Java"); PolymorphismExample.show(101.655); } } |
Output :
String Object Found
Undefined Type Object
1 ) Compile time polymorphism :- It is also known as static polymorphism or binding ,and in java it is achieved by Method Overloading.
2 ) Run time polymorphism :- It is also known as dynamic polymorphism or binding ,and in java it is achieved by Method Overriding.
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment