Java Programming Handbook

    Polymorphism using Overloading and Overriding in Java

    Polymorphism is one of the fundamental principles of Object-Oriented Programming (OOP). It allows a single interface to represent different types of actions. Java supports two types of polymorphism:

    • Compile-time polymorphism (Method Overloading)
    • Runtime polymorphism (Method Overriding)

    We have already covered Method Overriding and Dynamic Method Dispatch in detail. In this blog, we will explore both Method Overloading and how it works alongside Overriding in achieving polymorphism in Java.

    Method Overloading (Compile-Time Polymorphism)#

    Method overloading in Java allows multiple methods to have the same name but different parameters. The compiler determines which method to call based on the method signature (number and type of parameters).

    Example of Method Overloading#

    class MathOperations { // Method to add two integers int add(int a, int b) { return a + b; } // Method to add three integers int add(int a, int b, int c) { return a + b + c; } // Method to add two double numbers double add(double a, double b) { return a + b; } } public class OverloadingExample { public static void main(String[] args) { MathOperations obj = new MathOperations(); System.out.println("Sum of two integers: " + obj.add(5, 10)); System.out.println("Sum of three integers: " + obj.add(5, 10, 15)); System.out.println("Sum of two double values: " + obj.add(5.5, 2.2)); } }

    Expected Output:#

    Sum of two integers: 15 Sum of three integers: 30 Sum of two double values: 7.7

    Key Points about Method Overloading:#

    • Overloaded methods must have different parameter lists.
    • The return type can be different, but it does not determine overloading.
    • It improves code readability and reusability.
    • It occurs at compile time, making it compile-time polymorphism.

    Method Overriding (Runtime Polymorphism)#

    Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same signature as in the superclass.

    Example of Method Overriding#

    class Animal { void makeSound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Dog barks"); } } public class OverridingExample { public static void main(String[] args) { Animal myAnimal = new Dog(); // Upcasting myAnimal.makeSound(); // Calls the overridden method in Dog class } }

    Expected Output:#

    Dog barks

    Key Points about Method Overriding:#

    • The method name and parameters must match exactly with the superclass method.
    • The method in the subclass should have the same return type or a subtype.
    • It occurs at runtime, making it runtime polymorphism.
    • The @Override annotation is used to indicate an overridden method.

    Polymorphism using Overloading and Overriding#

    Both overloading and overriding contribute to achieving polymorphism in Java:

    • Method Overloading provides flexibility by allowing multiple methods with the same name but different parameters within the same class.
    • Method Overriding allows dynamic method invocation, enabling different behaviors based on the object type at runtime.

    Example Demonstrating Both Overloading and Overriding#

    class Shape { // Overloaded method (Compile-time polymorphism) void draw() { System.out.println("Drawing a shape"); } void draw(String shapeType) { System.out.println("Drawing a " + shapeType); } } class Circle extends Shape { // Overridden method (Runtime polymorphism) @Override void draw() { System.out.println("Drawing a Circle"); } } public class PolymorphismExample { public static void main(String[] args) { Shape shape1 = new Shape(); shape1.draw(); // Calls draw() from Shape shape1.draw("Square"); // Calls overloaded draw() from Shape Shape shape2 = new Circle(); // Upcasting shape2.draw(); // Calls overridden draw() from Circle } }

    Expected Output:#

    Drawing a shape Drawing a Square Drawing a Circle

    Conclusion#

    In this blog, we learned about Polymorphism in Java using Overloading and Overriding. We explored:

    • Method Overloading, which occurs at compile-time and enables multiple methods with the same name but different parameters.
    • Method Overriding, which allows runtime polymorphism by enabling a subclass to modify the behavior of a superclass method.
    • How both concepts together enable flexible and dynamic method execution.

    Polymorphism is a crucial concept in Java programming, making code more maintainable, scalable, and reusable. Understanding these concepts will help you write better object-oriented programs.

    Last updated on Apr 09, 2025