Polymorphism in Java
Polymorphism
Polymorphism is viewed as an essential element of Object-Oriented Programming. Polymorphism is the ability to perform a single action in multiple ways.
Polymorphism is a combination of two Greek words. Poly means "many," and morphs means "forms." So polymorphism signifies a variety of forms. Let's look at a real-world example of polymorphism.
Real-life example:
A person may have multiple characteristics at the same time.
A woman, for example, is a sister, a mother, a wife, and an employee all at the same time. As a result, the same person behaves differently in different contexts. This is known as polymorphism.
Types of Polymorphism in Java
In Java, there are two types of polymorphism:
- Compile-time polymorphism
- Runtime polymorphism
1. Compile-time Polymorphism:
Compile-time polymorphism, also known as static polymorphism, is a type of polymorphism that occurs during the compilation process. We can use method overloading to accomplish this form of polymorphism.
Method Overloading:
When several methods with the same name but different parameters exist in a class, these methods are said to be overloaded. The key benefit of method overloading is that it improves program readability. Methods can be overloaded by using different numbers of arguments or (and) by using different types of arguments.
i) With different numbers of arguments
In the example given below, we have written two methods: the first multiply() method, which takes two arguments and multiply the two numbers, and the second multiply() method, which takes three arguments and multiplies the three numbers. Consider the following example:
Example:
class MethodOverloadingExample {
// Function with two parameters
public int multiply(int num1, int num2) {
return num1 * num2;
}
// Function with three parameters
public int multiply(int num1, int num2, int num3) {
return num1 * num2 * num3;
}
}
public class Main {
public static void main(String args[]) {
MethodOverloadingExample obj = new MethodOverloadingExample();
// Method calling by passing arguments
int productOfTwoNumbers = obj.multiply(10, 20);
int productOfThreeNumbers = obj.multiply(10, 20, 30);
System.out.println(productOfTwoNumbers);
System.out.println(productOfThreeNumbers);
}
}
Output:
200
6000
ii) With different types of arguments
We've created two add() methods in the example given below, each with a different data type.
The first add() method requires two integer arguments, while the second add() method requires two double arguments. Consider the following example.
Example:
class MethodOverloadingExample {
// Function with integer parameters
public int multiply(int num1, int num2) {
return num1 * num2;
}
// Function with double parameters
public double multiply(double num1, double num2) {
return num1 * num2;
}
}
public class Main {
public static void main(String args[]) {
MethodOverloadingExample obj = new MethodOverloadingExample();
// Method calling by passing arguments
int productInt = obj.multiply(10, 20);
double productDouble = obj.multiply(10.0, 20.0);
System.out.println(productInt);
System.out.println(productDouble);
}
}
Output:
200
200.0
Runtime Polymorphism
Dynamic polymorphism is another name for runtime polymorphism. In Java, method overriding is a technique for implementing runtime polymorphism. It is also sometimes known as Dynamic method dispatch.
Method Overriding
Method overriding is a feature that allows you to redefine the base class method in the derived class depending on the requirement of the derived class. In other words, whatever methods are available in the base class are by default also available in the derived class. However, a derived class can be dissatisfied with the base class method implementation at times. Then, the derived class can redefine the method based on its requirements. This process is called method overriding.
Rules for Method Overriding:
- One class (derived class) should inherit another class (base class).
- The base class's method and the derived class's method must have the same name.
- The parameters of the base class's method and the derived class's method must be the same.
Example:
class Parent {
public void show()
{
System.out.println("Inside parent class");
}
}
class subclass1 extends Parent {
public void show()
{
System.out.println("Inside subclass1");
}
}
class subclass2 extends Parent {
public void show()
{
System.out.println("Inside subclass2");
}
}
public class Main {
public static void main(String args[])
{
Parent p;
// Upcasting
p = new subclass1();
p.show();
p = new subclass2();
p.show();
}
}
Output:
Inside subclass1
Inside subclass2
Method Hiding in Java
If a subclass defines a static method with the same signature as a static method in a superclass, the subclass method hides the superclass method. It is only possible to hide methods if both the parent and child classes have static methods.
Example:
class Parent {
public static void show() {
System.out.println("Inside parent class");
}
}
class Child extends Parent {
public static void show() {
System.out.println("Inside subclass");
}
}
public class Main {
public static void main(String args[]) {
Parent p = new Parent();
p.show();
Parent c = new Child();
c.show();
}
}
Output:
Inside parent class
Inside parent class
In the above example, we assume that p.show() will call the show() method from the parent class and c.show() will call the show() method from the child class, just like in overriding, but since show() is a static method, we have hidden the show() method rather than overriding it.