The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.
Polymorphism is considered as one of the important features of Object Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word âpolyâ means many and âmorphsâ means forms, So it means many forms.
In Java polymorphism is mainly divided into two types:
- Compile time Polymorphism
- Runtime Polymorphism
-
Compile time polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.

-
Method Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
- Example: By using different types of arguments
// Java program for Method overloadingclassMultiplyFun {// Method with 2 parameterstaticintMultiply(inta,intb){returna * b;}// Method with the same name but 2 double parameterstaticdoubleMultiply(doublea,doubleb){returna * b;}}classMain {publicstaticvoidmain(String[] args){System.out.println(MultiplyFun.Multiply(2,4));System.out.println(MultiplyFun.Multiply(5.5,6.3));}}chevron_rightfilter_noneOutput:8 34.65
- Example: By using different numbers of arguments
// Java program for Method overloadingclassMultiplyFun {// Method with 2 parameterstaticintMultiply(inta,intb){returna * b;}// Method with the same name but 3 parameterstaticintMultiply(inta,intb,intc){returna * b * c;}}classMain {publicstaticvoidmain(String[] args){System.out.println(MultiplyFun.Multiply(2,4));System.out.println(MultiplyFun.Multiply(2,7,3));}}chevron_rightfilter_noneOutput:8 42
- Example: By using different types of arguments
-
Operator Overloading: Java also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.
In java, Only “+” operator can be overloaded:
- To add integers
- To concatenate strings
Example:
// Java program for Operator overloadingclassOperatorOVERDDN {voidoperator(String str1, String str2){String s = str1 + str2;System.out.println("Concatinated String - "+ s);}voidoperator(inta,intb){intc = a + b;System.out.println("Sum = "+ c);}}classMain {publicstaticvoidmain(String[] args){OperatorOVERDDN obj =newOperatorOVERDDN();obj.operator(2,3);obj.operator("joe","now");}}chevron_rightfilter_noneOutput:Sum = 5 Concatinated String - joenow
-
Method Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
-
Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
-
Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
Example:
// Java program for Method overriddingclassParent {voidPrint(){System.out.println("parent class");}}classsubclass1extendsParent {voidPrint(){System.out.println("subclass1");}}classsubclass2extendsParent {voidPrint(){System.out.println("subclass2");}}classTestPolymorphism3 {publicstaticvoidmain(String[] args){Parent a;a =newsubclass1();a.Print();a =newsubclass2();a.Print();}}chevron_rightfilter_noneOutput:subclass1 subclass2
-
Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
Attention reader! Donât stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Dynamic Method Dispatch or Runtime Polymorphism in Java
- Difference between Compile-time and Run-time Polymorphism in Java
- Polymorphism in Python
- Scala | Polymorphism
- Perl | Polymorphism in OOPs
- Polymorphism in Ruby
- Understanding Encapsulation, Inheritance, Polymorphism, Abstraction in OOPs
- Polymorphism in R Programming
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1
- Java.io.InputStream Class in Java
- Java.io.SequenceInputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : bajracharyakshitij

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
