Polymorphism In Java With Examples

In this post, we will learn about polymorphism in Java and how it works. So, without wasting any time, let's get into the discussion.

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 the 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):

   - In Java we can achieve it through method overloading.


  • 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: 
Sum of 10 and 25 is: 35 
Sum of 5, 3, and 8 is: 16


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 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):

   -It can be achieved through method overriding.


  • What Is Method Overriding?

In 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();
        
        Dog dog = new Dog();
        dog.eat();
    }
}
OUTPUT: 
Animal is eating.. 
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.


    Related Topics:


  • Encapsulation In Java
  • Inheritance In Java
  • Abstraction In Java
  • OOPs Interview Questions
  • Strings Interview Questions
  • No comments:

    Post a Comment

    If you have any doubts, please discuss here...👇