The term "polymorphism" means having many forms. In simple terms, it refers to the ability of something to exist in different forms or have different behaviors. It allows us to perform a single action in different or multiple ways.
Here is a real-life example of Polymorphism:
In everyday life, a person can have different roles or characteristics. For example, a man can be a husband, a father, and an employee, all at the same time. In different situations, the same person may exhibit different behaviors. This is an example of polymorphism.There are two types of polymorphism in Java:
1. Compile-time Polymorphism (Also known as static polymorphism):
Compile-time polymorphism is also known as static polymorphism, we can achieve compile-time polymorphism using method overloading in Java. This simply means that the decision about which method to call is made at compile-time based on the method signature.
- What Is Method Overloading?
Within a class if there are multiple methods with the same name but with different parameters, Then they are considered as overloaded methods. It can be overloaded by changing the number or type of arguments.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
int sum1 = calculator.add(10, 25);
System.out.println("Sum of 10 and 25 is: " + sum1);
int sum2 = calculator.add(5, 3, 8);
System.out.println("Sum of 5, 3, and 8 is: " + sum2);
}
}
OUTPUT: In the above example, we can clearly see that we are overloading the method called add by changing the parameters. Method overloading allows us to define multiple methods in the same class with the same name but different parameter lists. In the Calculator class, we have two add methods - one that takes two integers and another that takes three integers. By changing the number of parameters, we achieve method overloading.
2. Runtime Polymorphism (Also known as dynamic polymorphism):
Run-time polymorphism is also known as dynamic polymorphism, It occurs when the method to be executed is decided during the execution of the program. We can achieve runtime polymorphism using method overriding. In runtime polymorphism the Java Virtual Machine (JVM) decides which method to call based on the runtime object type.
- What Is Method Overriding?
In a subclass, if we declare a method with the same name, same parameters, and the same return type as a method in the superclass, then it is called as method overriding.
class Animal{
public void eat(){
System.out.println("Animal is eating...");
}
}
class Dog extends Animal{
public void eat(){
System.out.println("Dog is eating...");
}
}
public class Main {
public static void main(String[] args){
Animal animal = new Animal();
animal.eat();
Animal dog = new Dog();
dog.eat();
}
}
OUTPUT: In the above code, Inside a main() method we can clearly see that we are using the Animal class as a reference to store the Dog class object and when we call the method eat() using that object it will call the method based on the runtime object which is Dog in this case. So the second call will print "Dog is eating...".
In conclusion, polymorphism in Java simply means the ability to have multiple forms or behaviors. It allows a single action to be performed in different ways. Java supports both compile-time & run-time polymorphism.
No comments:
Post a Comment
Share your thoughts or ask questions below!...👇