Open In App

Overloading vs Overriding in Java

Last Updated : 14 Oct, 2025
Comments
Improve
Suggest changes
164 Likes
Like
Report

Method Overloading and Method Overriding allow methods with the same name but different behavior, supporting polymorphism, the ability of one name to represent multiple forms.

method_overloading_compile_time_polymorphism_

Method Overloading (achieved at compile time)

Method Overloading occurs when we have multiple methods in the same class with the same name but have different numbers of parameters. It allows performing operations with different inputs.

Java
public class OverloadDemo {

    // Method 1: two int parameters
    static int my_Sum(int a, int b) { return a + b; }

    // Method 2: three int parameters
    static int my_Sum(int a, int b, int c)
    {

        return a + b + c;
    }

    public static void main(String[] args)
    {

        System.out.println("my_Sum with 2 int parameters: "
                           + my_Sum(4, 6));
                           
        System.out.println("my_Sum with 3 int parameters: "
                           + my_Sum(4, 6, 7));
    }
}

Output
my_Sum with 2 int parameters: 10
my_Sum with 3 int parameters: 17

Explanation: The my_Sum method is overridden in the child class. At runtime, Java determines which version to call based on the actual object. Calling my_Sum(5, 10) on a Calculator object uses the parent class method, while calling it on an AdvancedCalculator object uses the overridden child class method. This demonstrates runtime (dynamic) polymorphism.

Method Overriding (achieved at run time)

Method Overriding is redefining a superclass method in a subclass with the same signature and a compatible return type. It enables runtime polymorphism, where the JVM calls the method based on the actual object type.

Java
// Parent class
class Calculator {

    // Method to sum two integers
    int my_Sum(int a, int b){

        System.out.println("Parent class sum method:");
        return a + b;
    }
}

// Child class
class AdvancedCalculator extends Calculator{

    // Overriding the my_Sum method
    @Override int my_Sum(int a, int b){

        System.out.println(
            "Child class overridden sum method:");
        // Adding custom behavior
        return a + b
            + 10; // adds extra 10 for demonstration
    }
}

// Main class
public class OverrideDemo {
    public static void main(String[] args)
    {
        Calculator calc1 = new Calculator();
        System.out.println("Result: "
                           + calc1.my_Sum(5, 10));

        AdvancedCalculator calc2 = new AdvancedCalculator();
        System.out.println("Result: "
                           + calc2.my_Sum(5, 10));

        // Polymorphism example
        Calculator calc3 = new AdvancedCalculator();
        System.out.println("Result: "
                           + calc3.my_Sum(5, 10));
    }
}

Output
DerivedClass display() method
BaseClass show() method

Explanation: Even though bs is a BaseClass reference, it actually points to a DerivedClass object. So, when we call display(), Java runs the DerivedClass version because it’s overridden. The show() method wasn’t changed in the subclass, so calling it just runs the BaseClass version.

Method Overloading vs Method Overriding

The differences between Method Overloading and Method Overriding in Java are as follows:

FeatureMethod OverloadingMethod Overriding
DefinitionDefining multiple methods with the same name but different parameters in the same class.Redefining a parent class method in the subclass with the same signature and compatible return type.
PurposeTo achieve compile-time polymorphism (static binding).To achieve runtime polymorphism (dynamic binding).
Parameter ListMust be different (in number, type, or order).Must be exactly the same as in the parent class.
Return TypeCan be same or different, but must not conflict.Must be same or covariant (subtype of parent’s return type).
InheritanceNot required; can occur within the same class.Requires inheritance between superclass and subclass.
Access ModifierCan have any access modifier.Cannot reduce parent method’s access level.
Static / Final MethodsCan be overloaded.Cannot be overridden if marked static or final.
Binding TimeCompile-time binding.Runtime binding.
Exception HandlingOverloaded methods can declare any exceptions.Overridden method cannot throw broader checked exceptions than the parent.
Examplevoid add(int a, int b) and void add(double a, double b)Parent: void show() → Child: void show()

Difference Between Method Overloading and Method Overriding in Java

Explore