Published on

Polymorphism

Authors
  • avatar
    Name
    Iraianbu A
    Twitter

Table of Contents

Introduction

Polymorphism is one of the core principles of Object-Oriented Programming (OOP). It allows a single interface to be used for different types of objects, enabling flexibilty, scalability and code resue.

Benefits

  • Code Reusability : Allows the same class or methods to be used with different objects.
  • Flexibility : Object of different classes to be treated as objects of common superclass.
  • Abstraction : Simplifies the interaction with objects.
  • Dynamic Behavior : giving the program dynamic behavior based on the actual object type rather than the reference type.

Types of Polymorphism

  1. Compile-time Polymorphism (Method Overloading).
  2. Run-time Polymorphism (Method Overriding).

Compile-Time Polymorphism

Compile-time polymorphism is also known as static polymorphism and also know as method overloading. It occurs when multiple methods in the same class share the same name but have different method signatures (parameters). The method to be called is determined at compile time.

Java

class ConsoleOperation {
    public void print(int number) {
        System.out.println(number);
    }

    public void print(String text) {
        System.out.println(text);
    }

    public void print(int number, String text) {
        System.out.println(number + " " + text);
    }
}

public class Main {
    public static void main(String... args) {
        ConsoleOperation consoleOperation = new ConsoleOperation();
        consoleOperation.print(10);
        consoleOperation.print("Hello");
        consoleOperation.print(10, "Hello");
    }

Output

10
Hello
10 Hello

IMPORTANT

Java does not support operator overloading.

Run-Time Polymorphism

Run-time polymorphism is also known as Dynamic Method Dispatch and also know as method overriding. It occurs when a method is overridden in a subclass. The method to be called is determined at run time.

Java

class Sport {
    public void display() {
        System.out.println("Sport is played");
    }
}

class Football extends Sport {
    @Override
    public void display() {
        System.out.println("Football is played");
    }
}

class Cricket extends Sport {
    @Override
    public void display() {
        System.out.println("Cricket is played");
    }
}

public class Main {
    public static void main(String... args) {
        Sport sport = new Football(); // Upcasting
        sport.display();

        sport = new Cricket(); // Dynamic method dispatch
        sport.display();
    }
}

Output

Football is played
Cricket is played

NOTE

Upcasting (or widening) is the typecasting from a subclass to a superclass (Child to Parent class).
Downcasting (or narrowing) is the typecasting from a superclass to a subclass (Parent to Child class).

Polymorphism with Interfaces

Polymorphism is widely used with interfaces, allowing multiple classes to share a common contract.

Java

interface Payment{
    void pay();
}

class CreditCardPayment implements Payment{
    @Override
    public void pay(double amount) {
        System.out.println("Payment done using credit card: " + amount);
    }
}

class DebitCardPayment implements Payment{
    @Override
    public void pay(double amount) {
        System.out.println("Payment done using debit card: " + amount);
    }
}

public class Main {
    public static void main(String... args) {
        Payment payment = new CreditCardPayment();
        payment.pay(100.00);

        payment = new DebitCardPayment();
        payment.pay(200.0);
    }
}


Output

Payment done using credit card: 100.0
Payment done using debit card: 200.0

References