Understanding Polymorphism in Java: A Beginner's Guide

Polymorphism means many forms. Having one entity with multiple forms is called as polymorphism. There are two types of polymorphism.

1. Run time polymorphism.

2. Compile time polymorphism.

1. Run Time Polymorphism :

Call to overrided method is resolved during run time based on type of object is called runtime polymorphism. To acheive runtime polymorphism we must follow three rules.

1. Method Overriding.

2. Generalised method.

3. Upcasting.

For example paying money in Amazon is runtime polymorphism. It will have multiple options to pay money credit/debit card, Net Banking, Wallet, Cash on delivery etc. This is called run time polymorphism.

Example for Run Time Polymorphism :

interface Bike
{
   void start();
}
public class Hero implements Bike
{
  @Override
  public void start()
  {
    System.out.println("KickStart Hero");
  }
}
public class Duke implements Bike
{
  @Override
  public void start()
  {
     System.out.println("SelfStart Duke");
  }
}
Public class RoyalEnfield implements Bike
{
  @Override
  public void start()
  {
     System.out.println("Kickstart or Selfstart RoyalEnfield");
  }
}
public class Race
{
  public static void main(String [] args)
  {
     StartRace(new RoyalEnfield());//Upcasting from Generalised method
     StartRace(new Duke());//Upcasting from Generalised method
     StartRace(new Hero());//Upcasting from Generalised method
  }
  public static void StartRace(Bike b1) //Generalised method
  {
     b1.start();//overriding
  }
}

2. Compile Time Polymorphism :

Call to overloaded method is resolved during compile time based on type of arguments is called compile time polymorphism. To acheive compile time polymorphism we have to follow two rules.

1. Method Overloading.

2. Overloaded methods must be static.

Example for Compile Time Polymorphism :

class Multiplication {
    // Method to multiply two integers
    int multiply(int a, int b) {
        return a * b;
    }

    // Method to multiply three integers
    int multiply(int a, int b, int c) {
        return a * b * c;
    }

    // Method to multiply two doubles
    double multiply(double a, double b) {
        return a * b;
    }

    public static void main(String[] args) {
        Multiplication mul = new Multiplication();

        // Calling multiply method with two integers
        System.out.println("Product of 3 and 4: " + mul.multiply(3, 4));

        // Calling multiply method with three integers
        System.out.println("Product of 2, 3 and 4: " + mul.multiply(2, 3, 4));

        // Calling multiply method with two doubles
        System.out.println("Product of 5.5 and 6.5: " + mul.multiply(5.5, 6.5));
    }
}

Polymorphism, which means having one entity with multiple forms, is a fundamental concept in programming with two main types: run-time and compile-time polymorphism. Run-time polymorphism is achieved through method overriding, generalized methods, and upcasting, allowing method calls to be resolved based on object types at runtime. An example includes a payment system with multiple options. Compile-time polymorphism, on the other hand, involves method overloading and resolving method calls based on argument types during compilation. Examples include multiplying numbers using overloaded methods.

1
Subscribe to my newsletter

Read articles from Sravya Talabathula directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sravya Talabathula
Sravya Talabathula