This Articles Contents
What is Polymorphism ?
Polymorphism is the ability to perform different operation in different scenarios. Sometimes, the polymorphism is based on the input parameters to the method, and some time sequence of parameter.
Real time example
A delivery person transfers items to the address given. If it’s a mailman, letters and parcels will be sent to his house. If it’s a pizza delivery man, he’ll deliver you the pizza. The polymorphism in the distribution function is accomplished by the multiple executions here.
Polymorphism have 2 type
1. Compile time Polymorphism
2. Runtime Polymorphism
Compile time Polymorphism
It is also referring as static polymorphism. It’s called compile-time polymorphism because the compiler is able to evaluate the actual function. Compile time Polymorphism is feature categories into 2 type.
- Method Overloading
- Operator Overloading
Method Overloading
It is called process overloading if different functions in a class have the same name but different signature. A signature of a method includes arguments for the name and method. So, overwhelmed strategies have multiple arguments. In numbers or the form of arguments, the arguments may vary. The return type of the system does not form part of the signature.
package com.onurdesk.oop.polymorphism;
public class Printer {
void read() {
System.out.println("Reading from memory");
}
void read(String str) {
System.out.println("Reading from memory with String");
}
void read(String str, int i) {
System.out.println("Reading from memory with String with number");
}
}
You can find a lot of them in the String and primitive wrapper classes if you are searching for method overload in JDK classes. For example, String getBytes() methods, Integer getChars() methods, etc.
Operator Overloading
Java doesn’t encourage operators to bypass us. However, for string objects, the addition (+) operator is overloaded. When used on string objects, the extension operator concatenates them and returns a new string object.
Runtime Polymorphism
The polymorphism of the runtime is accomplished by overriding of the method. If the method of the superclass in the subclass is overridden, it is called the overriding method.
package com.onurdesk.oop.polymorphism;
class Parent{
public void testMethod(){
System.out.println("Overridden Method");
}
}
public class Test extends Parent{
public void testMethod(){
System.out.println("Overriding Method");
}
public static void main(String args[]){
Parent obj = new Test();
obj.testMethod();
}
}
Difference between Overloading and Overriding
Overloading of method ( Compile time polymorphism) | Overriding of method ( Runtime polymorphism) |
If a class has the same method name with a different argument, so that is known as method overloading. | Overriding method – In the subclass, the superclass process is overridden in order to have more specific implementation. |
The method is overloaded by keeping the same method name and only changing the number of arguments. | In Method overriding-The superclass method is overridden in the subclass when the subclass method in java is overridden. |
Method name – method has same name. Access modifier – It doesn’t matter. Return type – It doesn’t matter. Number of parameters in java – It have different number of parameters Exception thrown – It doesn’t matter. | Method name – method has same name as in parent class. Access modifier – It must not be restrictive form parent class. Return type – Java allows overriding by modifying the return type, but only the return type of the covariant is permitted in Java. Number of parameters in java – It must have same number of parameters in java. Exception thrown –
|
We can also overload main method in java | We cannot overridde main method as it is a static method. |
We can overload private method in java | We cannot overridde private method |
We can overload final method | We cannot override final method |