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.
Top comments (0)