Java

Polymorphism In Java

One OOPs feature that enables us to carry out a single operation in several ways is polymorphism. As an example, let’s imagine that the class Animal contains a method called sound (). We are unable to offer this generic class an implementation like: Roar, Meow, Oink, etc. because of its nature. We had to provide an all-purpose message.

public class Animal{
   ...
   public void sound(){
      System.out.println("Animal is making a sound");   
   }
}

Let’s imagine that the Animal class expands to include the subclasses Horse and Cat (see Inheritance). We may provide the same method’s implementation in the following way:

public class Horse extends Animal{
...
    @Override
    public void sound(){
        System.out.println("Neigh");
    }
}

and

public class Cat extends Animal{
...
    @Override
    public void sound(){
        System.out.println("Meow");
    }
}

As you can see, there were several methods to carry out the same action even though sound() was the common action for all subclasses. This is the ideal illustration of polymorphism (feature that allows us to perform a single action in different ways). Due to the fact that each Animal has a unique sound, it would be absurd to just use the general sound() function. Thus, we may conclude that this method’s action is dependent upon the kind of object.

What is polymorphism in programming?

The capacity of a method to operate in several ways depending on the object it is operating upon is known as polymorphism. Polymorphism, in other terms, enables you to create a single interface and have different implementations of it. We developed the function sound() and have many implementations of it in the various-2 sub classes, as can be seen in the sample above.
The example we provided above is a runtime polymorphism example since the choice of which sound() function will be invoked is made at runtime.

Example 1: Polymorphism in Java

Runtime Polymorphism example:

Animal.java

public class Animal{
   public void sound(){
      System.out.println("Animal is making a sound");   
   }
}

Horse.java

class Horse extends Animal{
    @Override
    public void sound(){
        System.out.println("Neigh");
    }
    public static void main(String args[]){
    	Animal obj = new Horse();
    	obj.sound();
    }
}

Output:

Neigh

Cat.java

public class Cat extends Animal{
    @Override
    public void sound(){
        System.out.println("Meow");
    }
    public static void main(String args[]){
    	Animal obj = new Cat();
    	obj.sound();
    }
}

Output:

Meow

Example 2: Compile time Polymorphism

On the other side, method overloading is an example of compile-time polymorphism.

class Overload
{
    void demo (int a)
    {
       System.out.println ("a: " + a);
    }
    void demo (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double demo(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        Overload Obj = new Overload();
        double result;
        Obj .demo(10);
        Obj .demo(10, 20);
        result = Obj .demo(5.5);
        System.out.println("O/P : " + result);
    }
}

Demo() is overloaded three times in this case: the first overload has one integer argument, the second overload has two integer parameters, and the third overload contains a double parameter. The parameters we supply when calling methods decide which method is to be called. Compile time polymorphism is the name given to this sort of polymorphism since it occurs at the time of runtime compilation.

Output:

a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25

 

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button