Java Polymorphism: Compile time and Runtime Polymorphism

09 Feb 2023
Intermediate
249 Views

Java Polymorphism: Compile time and Runtime Polymorphism

What is Polymorphism?

Polymorphism is one of the most important and unique features in programming. In the word Polymorphism "Poly" stands for many and "Morph" stands for forms. Thus, it means many forms. As an example, we can be an engineer and a mother, a sister, and a daughter as well. Here, a single woman is performing various roles in life therefore Polymorphism performs the same role in programming.

What is Polymorphism in Java?

Polymorphism in Java is an utmost importance feature in Java. It is also one of the essential pillars of object-oriented programming Java. It represents a single entity in multiple forms such as constructor overloading, method overloading, etc.

Different types of Polymorphism in Java

There are two types of Polymorphism in java, which are

  1. Compile time Polymorphism
  2. Run time Polymorphism

Compile time Polymorphism in Java

Compile time polymorphism in Java resolve the problem regarding compile time with the help of "Method overloading" and "Constructor overloading".

Method overloading in Java

Method overloading in Java works for two or more methods or functions that are stored in the same class with the same name but different parameters and arguments.

Example

class Method_Overloading
{
    //Method Overloading by changing the number of arguments (or parameters)

//Method 1
    double figure(double l, double b) //two arguments or parameters
    {
        return (l*b);
    }
    double figure(double s) //one argument or parameter
    {
        return (s*s);
    }

//Method 2
    public static void main(String[] args)
    {
        Method_Overloading obj = new Method_Overloading();
        System.out.println("Area of Rectangle: " +obj.figure(5.55, 6.78));
        System.out.println("Area of Square: " +obj.figure(3.45));
    }
}

Output:

Area of Rectangle: 37.629

Area of Square: 11.90250000000002

Operator overloading in Java:

Operator overloading in the programming language Java is a method of programming where operators are implemented in the type of user-defined program with specific logic that is dependent on the types of given arguments. Operator overloading in a java programming language makes the program easier as it specifies a user-defined implementation for operations with the help of one or both operands of a user-defined structure or class.

Example

// Java program to demonstrate the
// working of operator overloading
public class SCHOLAR-HAT 
          {
 // function for adding two integers
 void add(int a, int b)
 {
  int sum = a + b;
  System.out.println(" Addition of two integer :"
      + sum);
 }

 // function for concatenating two strings
 void add(String s1, String s2)
 {
  String con_str = s1 + s2;
  System.out.println("Concatenated strings :"
      + con_str);
 }
 public static void main(String args[])
 {
  SCHOLAR-HAT obj = new SCHOLAR-HAT();
  // addition of two numbers
  obj.add(10, 10);
  // concatenation of two string
  obj.add("Operator ", " overloading ");
 }
}

 Output

Addition of two integer :20
Concatenated strings :Operator overloading 

Runtime Polymorphism in Java

Runtime Polymorphism specifies the depiction of run time by using overriding.

Method overriding in Java

This particular method in Java redefines a superclass method by generating the same name, parameters, and data types in a subclass.

Example

//A Simple made up Instance to show method overriding in Java
class Waka_Waka_Song
{
    Waka_Waka_Song()
    {
        System.out.println("Song--Initializing Waka Waka by Shakira...");
    }
    public void play()
    {
        System.out.println("Song-- Playing...");
    }
}

//Spotify wants Waka Waka song on their Platform
class Spotify extends Waka_Waka_Song
{
    Spotify()
    {
        System.out.println("Spotify...");
    }
    public void play()
    {
        System.out.println("Spotify-- Playing.. ");
    }
}

//Amazon_Music wants Waka Waka song on their Platform
class Amazon_Music extends Waka_Waka_Song
{
    Amazon_Music()
    {
        System.out.println("Amazon Music...");
    }
    public void play()
    {
        System.out.println("Amazon Prime-- Playing...");
    }
}

//A User Playing songs on Spotify and then Amazon Music
class User
{
    public static void main(String[] args)
    {
        System.out.println("Playing Waka Waka on Spotify... ");
        Spotify s = new Spotify();
        s.play();
        System.out.println("\nPlaying Waka Waka on Amazon Music");
        Amazon_Music p = new Amazon_Music();
        p.play();
    }
}

Output

Playing Waka Waka on Spotify…

Song - - Initializing Waka Waka by Shakira…

Spotify…

Spotify- - Playing . .

Playing Waka Waka on Amazon Music

Song - - Initializing Waka Waka by Shakira…

Amazon Music …

Amazon Prime- - Playing …

Summary

In this article, the definition of polymorphism in Java including its type has been discussed vastly. How can you achieve runtime polymorphism has also been highlighted in this article. The definition of Runtime and Compile time Polymorphism with examples has been evaluated in this article.

Accept cookies & close this