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

Vigneshwaralingam @vigneshwaralingam

About: Computer Science Engineering student passionate about software development. Currently exploring Java, PSQL, , and web development. Always eager to learn and

Location:
Tuticorin, Tamil Nadu, India.
Joined:
Mar 17, 2025

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

Publish Date: Apr 8
1 0

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

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

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

🔤 B. Bank – Deposit system

void deposit(int amount) { }
void deposit(String chequeNumber) { }
Enter fullscreen mode Exit fullscreen mode

🔤 C. Calculator

int calc(int a, int b) { return a + b; }
double calc(double a, double b) { return a * b; }
Enter fullscreen mode Exit fullscreen mode

🔤 D. Display

void display(String name) { }
void display(String name, int age) { }
Enter fullscreen mode Exit fullscreen mode

🔤 E. Email Sender

void send(String to) { }
void send(String to, String subject) { }
void send(String to, String subject, String msg) { }
Enter fullscreen mode Exit fullscreen mode

🔤 Z. Zipping files (hypothetical)

void zip(String file) { }
void zip(String[] files) { }
void zip(String file, String destination) { }
Enter fullscreen mode Exit fullscreen mode

⚙ 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)
Enter fullscreen mode Exit fullscreen mode

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

🧠 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.

Comments 0 total

    Add comment