Overloading vs Overriding in Java (original) (raw)

Last Updated : 8 Jun, 2026

Method Overloading and Method Overriding are two important concepts in Java that enable polymorphism. Overloading allows multiple methods with the same name but different parameters within the same class, while overriding allows a subclass to provide a specific implementation of a method already defined in its parent class.

method_overloading_compile_time_polymorphism_

Method Overloading (Compile-Time Polymorphism)

Method Overloading occurs when multiple methods in the same class have the same name but different parameter lists. It allows a method to perform similar operations with different types or numbers of inputs.

public class OverloadDemo {

static int my_Sum(int a, int b) {
    return a + b;
}

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 overloaded by defining multiple methods with the same name but different parameter lists. The compiler chooses the appropriate method based on the number of arguments passed. This demonstrates compile-time polymorphism.

Method Overriding (Runtime Polymorphism)

Method Overriding occurs when a subclass provides its own implementation of a method already defined in the parent class. It allows the same method to behave differently based on the object's actual type.

// Parent class class Calculator {

public int my_Sum(int a, int b) {

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

}

// Child class class AdvancedCalculator extends Calculator {

@Override
public int my_Sum(int a, int b) {

    System.out.println(
        "Child class overridden sum method:");

    return a + b + 10;
}

}

// 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));

    // Runtime Polymorphism
    Calculator calc3 =
        new AdvancedCalculator();

    System.out.println(
        "Result: " + calc3.my_Sum(5, 10));
}

}

`

Output

Parent class sum method: Result: 15 Child class overridden sum method: Result: 25 Child class overridden sum method: Result: 25

**Explanation: The child class overrides the my_Sum() method of the parent class. When a Calculator reference points to an AdvancedCalculator object, Java calls the overridden method at runtime. This demonstrates runtime polymorphism (dynamic method dispatch).

Method Overloading vs Method Overriding

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

Feature Method Overloading Method Overriding
**Definition Defining 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.
**Purpose To achieve compile-time polymorphism (static binding). To achieve runtime polymorphism (dynamic binding).
**Parameter List Must be different (in number, type, or order). Must be exactly the same as in the parent class.
**Return Type Can be same or different, but must not conflict. Must be same or covariant (subtype of parent’s return type).
**Inheritance Not required; can occur within the same class. Requires inheritance between superclass and subclass.
**Access Modifier Can have any access modifier. Cannot reduce parent method’s access level.
**Static / Final Methods Can be overloaded. Cannot be overridden if marked static or final.
**Binding Time Compile-time binding. Runtime binding.
**Exception Handling Overloaded methods can declare any exceptions. Overridden method cannot throw broader checked exceptions than the parent.
**Example void add(int a, int b) and void add(double a, double b) Parent: void show() → Child: void show()