Java Polymorphism

Java Polymorphism is one of the most important concepts of Object-Oriented Programming (OOP) that allows you to use a single entity to represent multiple forms. Polymorphism in Java is mainly categorized into two types: Compile-time polymorphism and Runtime polymorphism. In this blog, we will explore both types of polymorphism in Java, including how to declare and use them, and provide example code snippets to illustrate each concept.

Compile-time Polymorphism:

Compile-time polymorphism in Java is also known as method overloading. It allows you to define multiple methods with the same name in the same class, but with different parameters. Here is an example:

public class ExampleClass {
   static int sum(int num1, int num2) {
      return num1 + num2;
   }

   static int sum(int num1, int num2, int num3) {
      return num1 + num2 + num3;
   }
}

In the above example, we have declared two methods with the same name “sum,” but with different parameters. Depending on the number of arguments passed to the method, the appropriate method will be called.

To call these methods, we can do the following:

int total1 = ExampleClass.sum(5, 7);
int total2 = ExampleClass.sum(5, 7, 3);
System.out.println(total1);
System.out.println(total2);

In the above example, we call the sum method with two arguments and three arguments, respectively, and assign the returned value to the variables total1 and total2. We then print the total1 and total2 values, which are 12 and 15, respectively.

Runtime Polymorphism:

Runtime Polymorphism in Java is also known as method overriding. It allows you to define a method in a subclass with the same name, return type, and parameter types as the method in the parent class. Here is an example:

class Animal {
   void animalSound() {
      System.out.println("The animal makes a sound");
   }
}

class Cat extends Animal {
   void animalSound() {
      System.out.println("The cat meows");
   }
}

class Dog extends Animal {
   void animalSound() {
      System.out.println("The dog barks");
   }
}

In the above example, we declare a parent class Animal with a method called animalSound. We then declare two child classes, Cat and Dog, that override the animalSound method with their own implementation.

To call these methods, we can do the following:

Animal myAnimal = new Animal();  
Animal myCat = new Cat();  
Animal myDog = new Dog();  

myAnimal.animalSound();  
myCat.animalSound();  
myDog.animalSound();

In the above example, we create three objects of the Animal, Cat, and Dog classes, respectively. We then call the animalSound method on each object. Since each object has its own implementation of the animalSound method, the appropriate method will be called, resulting in the output:

The animal makes a sound
The cat meows
The dog barks

Conclusion:

Java Polymorphism is an essential part of Java programming, allowing you to use a single entity to represent multiple forms. With the examples provided, you can get started with Java Polymorphism and take your programming skills to the next level.