Awesome! Let’s dive into Day 9 of your Java programming journey, covering Method Overloading.

🚀 Java Programming – Day 9: Method Overloading from A to Z


📘 What is Method Overloading?

Method Overloading means creating multiple methods with the same name in the same class, but with different parameters (type, number, or order).

👉 It helps write clean, readable code by logically grouping related actions under one method name.


📌 Syntax:

class Demo {
    void show() {
        System.out.println("No parameters");
    }

    void show(int a) {
        System.out.println("Integer: " + a);
    }

    void show(String b) {
        System.out.println("String: " + b);
    }
}

🧠 Why Method Overloading?

  • Makes code more readable
  • Avoids method name confusion
  • Implements polymorphism (compile-time)
  • Useful in real-world scenarios like input validation, data formatting, calculations, etc.

🔁 How Overloading Works:

✅ Valid Overloading:

Criteria Example
Different number of params add(int a) vs add(int a, int b)
Different type of params add(int a) vs add(float a)
Different order of params add(int a, float b) vs add(float a, int b)

❌ What is NOT Overloading?

  • Changing only the return type is NOT overloading.
int show() { }  
float show() { } // ❌ Compile-time error: same signature

🧪 Examples – A to Z

🔤 A. Adding numbers

int add(int a, int b) {
    return a + b;
}

float add(float a, float b) {
    return a + b;
}

🔤 B. Bank – Deposit system

void deposit(int amount) { }
void deposit(String chequeNumber) { }

🔤 C. Calculator

int calc(int a, int b) { return a + b; }
double calc(double a, double b) { return a * b; }

🔤 D. Display

void display(String name) { }
void display(String name, int age) { }

🔤 E. Email Sender

void send(String to) { }
void send(String to, String subject) { }
void send(String to, String subject, String msg) { }

🔤 Z. Zipping files (hypothetical)

void zip(String file) { }
void zip(String[] files) { }
void zip(String file, String destination) { }

⚙ How Java Resolves Overloaded Methods:

  • Based on method signature
  • Uses automatic type conversion if needed
void print(double d) { }
void print(String s) { }

print(10); // matches double (int → double)

⚠ Method Overloading vs Method Overriding:

Feature Overloading Overriding
Compile-time or Runtime Compile-time Runtime
Same class? Yes No (Subclass required)
Parameters Must be different Must be same
Return type Can be same or different Must be same or covariant type

✅ Real-Life Example:

class Greet {
    void hello() {
        System.out.println("Hello!");
    }

    void hello(String name) {
        System.out.println("Hello, " + name);
    }

    void hello(String name, int age) {
        System.out.println("Hello " + name + ", age: " + age);
    }
}

🧠 Conclusion – What I Understood:

  • Method overloading is like giving multiple entry points for a single behavior.
  • Java allows overloading based on parameter types and counts.
  • Overloading increases readability, flexibility, and code reuse.
  • Java does not allow overloading by return type only.