Polymorphism is mainly derived from two Greek words: poly which means many and “morphs” means forms. So the polymorphism means many forms.
Types of Polymorphism in Java
Mainly, there are two types of polymorphism in Java.
- Compile-time polymorphism
- Runtime polymorphism
Which we can perform polymorphism in java by method overloading and method overriding. If we overload a static method in Java, it is an example of compile-time polymorphism.
Compile-time polymorphism in Java
Compile-time polymorphism also known as static polymorphism. This type of polymorphism is achieved by the function overloading or operator overloading.
Method Overloading: When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by a change in the number of arguments or/and changes in the type of arguments.
// Method overloading in java program class MultipleWay { // Method with 2 parameter static int Multiply(int a, int b) { return a * b; } // Method with the same name but 2 double parameter static double Multiple(double a, double b) { return a * b; } } public class Mains { public static void main(String[] args) { System.out.println(MultipleWay.Multiple(2, 8)); System.out.println(MultipleWay.Multiple(6.5, 8.3)); } }
Output : 16.0 53.95
Operator Overloading: Basically, Java also provides the option to overload operators. For example, if we can make the operator (‘+’) for string class to concatenate two strings. We know that it is the addition of an operator whose task is to add two operands.
In java, only “+” operator can be used for overloaded:
- For add integers
- For concatenate strings
// Operator overloading for Java Program class OperatorOVERDDN { void operators(String str1, String str2) { String s = str1 + str2; System.out.println("Concatinated String - " + s); } void operators(int a, int b) { int c = a + b; System.out.println("Sum = " + c); } } public class Mains { public static void main(String[] args) { OperatorOVERDDN obj = new OperatorOVERDDN(); obj.operators(2, 3); obj.operators("poe", "cod"); } }
Runtime Polymorphism in Java
The Runtime polymorphism in java also known as Dynamic Method Dispatch which is the process in which a call to an overridden method is resolved at runtime rather than compile-time.
In this process mainly an overridden method is called through the reference variable of a superclass. And this method to be called is based on the object being referred to by the reference variable.
Mainly, this type of polymorphism is achieved by Method Overriding.
- Method overriding only occurs when a derived class has a definition for one of the member functions of the base class. That base function also said to be overridden.
// Method overridding for Java program class Parent { void Print() { System.out.println("parent class"); } } class suberclass1 extends Parent { void Print() { System.out.println("I'm class 1"); } } class suberclass2 extends Parent { void Print() { System.out.println("I'm class 2"); } } public class TestPolymorphism { public static void main(String[] args) { Parent a; a = new suberclass1(); a.Print(); a = new suberclass2(); a.Print(); } } Output : I'm class 1 I'm class 2
In the next articles, we tell you about something more about java and it’s topics in a very interesting way so you can easily learn easily with fun:
- Abstraction in Java
- Encapsulation In detail with example
- Exception Handling in Java
- Collection Interface in Java
- Collections Class in Java
- Collection Algorithms with all sorting examples
- Generics
- Java – Multithreading
- Java – Files and I/O
- Java – Date & Time
- Java – Regular Expression