DEV Community

Kanishka Shrivastava
Kanishka Shrivastava

Posted on

#java #oop #programming #100daysofcode

Java Concepts I’m Mastering – Part 5: Method Overloading

Continuing my journey of mastering Java fundamentals while building real projects.

Today’s concept: Method Overloading.

What is Method Overloading?

Method Overloading means:

Same method name, but different parameters.

Java decides which method to call based on:

Number of parameters

Type of parameters

Order of parameters

Example

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Same method name → add
Different parameter lists → Overloading

Why is it Useful?

Improves code readability

Makes APIs cleaner

Supports compile-time polymorphism

Important Rule

You cannot overload methods by only changing the return type.

This is invalid:

int add(int a, int b)
double add(int a, int b)   // Error

Enter fullscreen mode Exit fullscreen mode

Parameters must differ.

What I Learned

Overloading makes code flexible

It’s resolved at compile-time

It’s one of the foundations of Polymorphism

Understanding this makes class design much stronger.

Next in the series: Method Overriding & Runtime Polymorphism

Top comments (0)