Programming & Development / April 14, 2025

Polymorphism in Java: Method Overloading vs Method Overriding Explained

Java polymorphism method overloading method overriding compile-time polymorphism runtime polymorphism polymorphism example in Java OOP principles Java

Polymorphism is one of the four core concepts of Object-Oriented Programming (OOP) in Java, alongside encapsulation, inheritance, and abstraction. At its core, polymorphism allows objects to take on multiple forms, enabling flexible and scalable code.

πŸ”„ What is Polymorphism in Java?

Polymorphism in Java enables a single interface or method to operate on different types of data. It makes code extensible, easier to maintain, and allows you to write generalized code that works across different object types.

Java supports two types of polymorphism:

  1. Compile-Time Polymorphism (Static Binding)
  2. Runtime Polymorphism (Dynamic Binding)

βœ… 1. Compile-Time Polymorphism (Method Overloading)

Definition:

This occurs when multiple methods in the same class have the same name but different parameter lists (type, number, or order).

Key Characteristics:

  • Happens at compile time.
  • Also called method overloading.
  • Does not require inheritance.

Example:

java

public class Calculator {
    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;
    }
}

In this example, the add method is overloaded three times to handle different input types and counts.

βœ… 2. Runtime Polymorphism (Method Overriding)

Definition:

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

Key Characteristics:

  • Happens at runtime.
  • Requires inheritance.
  • Uses method overriding.
  • Enables dynamic method dispatch (calling methods based on the object's actual type at runtime).

Example:

java

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

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

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows");
    }
}
java

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();    // Polymorphism in action
        Animal myCat = new Cat();

        myDog.makeSound(); // Output: Dog barks
        myCat.makeSound(); // Output: Cat meows
    }
}

Here, makeSound() behaves differently depending on the actual object type (Dog, Cat), even though it's called on a reference of type Animal.

πŸ“Š Comparison Table: Overloading vs Overriding

FeatureMethod OverloadingMethod OverridingOccurs AtCompile-timeRuntimeClass RelationshipSame classSubclass & Superclass (Inheritance)Method SignatureMust differMust be the sameAccess ModifiersNo restrictionCan't reduce visibilityReturn TypeCan differShould be the same or covariantKeyword UsedNone@Override annotation (optional)

🧠 Why Use Polymorphism?

  • Promotes code reusability and flexibility
  • Enables loose coupling between components
  • Makes code easier to extend and maintain
  • Supports the Open/Closed Principle (Open for extension, closed for modification)

πŸ“ Conclusion

Polymorphism in Java is a powerful OOP feature that allows objects to exhibit different behaviors depending on their type. Mastering both compile-time and runtime polymorphism is key to writing clean, extensible, and robust Java applications.

Whether you’re designing a flexible API, implementing a plugin system, or simply organizing your codebase better β€” polymorphism makes your Java code smarter and future-proof.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex