C

Core Java tutorial for beginners

Clean • Professional

Java - Method Overloading and Overriding (With Examples & Key Differences)

5 minute

Method Overloading & Overriding in Java

In Java, Method Overloading and Method Overriding are core OOP concepts used to achieve Polymorphism — the ability of a method to behave differently based on context. Both make programs flexible, readable, and reusable.


What is Method Overloading (Compile-Time Polymorphism)?

Occurs within the same class, when multiple methods have the same name but different parameters (type, number, or order).

  • Decision made at compile time → Compile-time polymorphism.

Syntax

class Calculator {
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }
    int add(int a, int b, int c) { return a + b + c; }
}

Example – Method Overloading

public class OverloadingExample {
    public static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println("Sum (int): " + calc.add(10, 20));
        System.out.println("Sum (double): " + calc.add(5.5, 4.5));
        System.out.println("Sum (3 ints): " + calc.add(1, 2, 3));
    }
}

Output:

Sum (int): 30
Sum (double): 10.0
Sum (3 ints): 6

Points About Overloading

  • Must have different parameter list (number, type, order).
  • Can have different return types (if parameter list differs).
  • Can overload constructors.
  • Happens within the same class.
  • Compile-time polymorphism.

Example – Constructor Overloading

class Student {
    String name; int age;

    Student(String name) { this.name = name; }
    Student(String name, int age) { this.name = name; this.age = age; }

    void show() { System.out.println(name + " - " + age); }
}

public class ConstructorOverloading {
    public static void main(String[] args) {
        Student s1 = new Student("Aman");
        Student s2 = new Student("Neha", 20);

        s1.show(); s2.show();
    }
}

Output:

Aman - 0
Neha - 20

What is Method Overriding (Runtime Polymorphism) ?

Occurs between parent and child classes, when a subclass provides a new implementation of a method already defined in its superclass.

  • Decision made at runtime → Runtime polymorphism.

Syntax

class Parent { void show() { ... } }
class Child extends Parent { @Override void show() { ... } }

Example – Method Overriding

class Animal {
    void sound() { System.out.println("Some sound"); }
}

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

public class OverridingExample {
    public static void main(String[] args) {
        Animal a = new Dog(); // Upcasting
        a.sound();            // Calls Dog's version
    }
}

Output:

Dog barks

Points About Overriding

  • Must have the same method name, parameters, and return type.
  • Must be in subclass (inheritance required).
  • The @Override annotation is optional but recommended.
  • The access level cannot be more restrictive than the parent.
  • Static, private, or final methods cannot be overridden.
  • Decision made at runtimeRuntime polymorphism.

Example – Using super Keyword

class Parent {
    void display() { System.out.println("Display from Parent"); }
}

class Child extends Parent {
    @Override
    void display() {
        super.display(); // call parent method
        System.out.println("Display from Child");
    }
}

public class SuperExample {
    public static void main(String[] args) {
        Child obj = new Child();
        obj.display();
    }
}

Output:

Display from Parent
Display from Child

Difference Between Overloading and Overriding

FeatureMethod OverloadingMethod Overriding
DefinitionSame method name, different parameters (same class)Same method name & parameters (subclass redefines)
Polymorphism TypeCompile-timeRuntime
ParametersMust differMust be same
Return TypeCan differ (if parameter list changes)Must be same or covariant
Access ModifierCan changeCan’t be more restrictive
Static/Final MethodsCan overloadCannot override
Keyword Used@Override
Occurs InSame classParent–child classes

When to Use Which

ScenarioUse
Multiple ways to perform similar operationsOverloading
Modify inherited behaviorOverriding
Polymorphic behavior with inheritanceOverriding

 

Article 0 of 0