Polymorphism in Java-Object-Oriented Programming with exmple

7/25/2025

Diagram of Polymorphism in Java-Object-Oriented Programming with exmple

Go Back

Understanding Polymorphism in Java: Method Overloading and Overriding Explained

Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to take many forms. In Java, polymorphism enables the same method name to behave differently based on context. There are two main types of polymorphism in Java:

  • Compile-time Polymorphism (Method Overloading)

  • Runtime Polymorphism (Method Overriding)

This article will explore both types of polymorphism with real-world examples to help you write flexible and reusable Java code.

 Diagram of Polymorphism in Java-Object-Oriented Programming with exmple

What is Polymorphism in Java?

Polymorphism comes from the Greek words poly (many) and morph (form). In Java, polymorphism allows methods to perform different tasks based on the object that invokes them or the number and type of arguments passed.


1. Method Overloading (Compile-Time Polymorphism)

Definition:

Method Overloading occurs when two or more methods in the same class have the same name but different parameters (number, order, or type of arguments).

Example:

public class Calculator {
    // Overloaded add method
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }

    public int add(int a, int b, int c) {
        return a + b + c;
    }
}

Key Points:

  • Happens at compile-time.

  • Improves code readability.

  • Same method name, but different method signatures.


2. Method Overriding (Runtime Polymorphism)

Definition:

Method Overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.

Example:

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

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

Usage:

Animal obj = new Dog();
obj.sound();  // Output: Dog barks

Key Points:

  • Happens at runtime.

  • Requires inheritance and use of the @Override annotation.

  • Enables dynamic method dispatch.


 

Real-world Example

Imagine building a payment system:

class Payment {
    public void processPayment() {
        System.out.println("Processing generic payment");
    }
}

class CreditCardPayment extends Payment {
    @Override
    public void processPayment() {
        System.out.println("Processing credit card payment");
    }
}

class PayPalPayment extends Payment {
    @Override
    public void processPayment() {
        System.out.println("Processing PayPal payment");
    }
}

Using polymorphism:

Payment payment = new CreditCardPayment();
payment.processPayment();  // Output: Processing credit card payment

This approach lets you swap out payment types without changing the business logic.


Conclusion

Polymorphism is a powerful feature in Java that improves code clarity, flexibility, and maintainability. By understanding method overloading and method overriding, you can write more efficient object-oriented applications. Whether you're building a payment system or designing a game, leveraging polymorphism will help you structure your code better.

Table of content