Java में Method Overloading
In this page:
static returnType methodName(typeA a) { ... }
static returnType methodName(typeA a, typeB b) { ... } // same name, different parameters
Overloading by Parameter Count
Overloading कई methods को बिल्कुल वही नाम share करने देता है जब तक उनकी parameter lists अलग हों -- यहां, purely इसमें अलग कि हर version कितने parameters accept करता है, जैसे add(int, int) बनाम add(int, int, int)।
उदाहरण: Overloading by Parameter Count
// Define the class Main
public class Main {
static int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
static int add(int a, int b, int c) {
// Send a + b + c back to the caller
return a + b + c;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(add(1, 2));
// Print a line to the console
System.out.println(add(1, 2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Overloading by Parameter Type
आप वही parameter count रखते हुए लेकिन types बदलकर भी overload कर सकते हैं, जैसे add(int, int) बनाम add(double, double), एक method name को naturally कई अलग तरह के input handle करने देते हुए।
उदाहरण: Overloading by Parameter Type
// Define the class Main
public class Main {
static int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
static double add(double a, double b) {
// Send a + b back to the caller
return a + b;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(add(1, 2));
// Print a line to the console
System.out.println(add(1.5, 2.5));
}
}
Login to try C/C++/Java/PHP code in the editor
Overloading by Parameter Order
वही types और count रखते हुए भी लेकिन उनका order swap करना, जैसे process(String, int) बनाम process(int, String), compiler के हिसाब से एक distinct, valid overload माना जाता है।
उदाहरण: Overloading by Parameter Order
// Define the class Main
public class Main {
static void process(String s, int n) {
// Print a line to the console
System.out.println(s + n);
}
static void process(int n, String s) {
// Print a line to the console
System.out.println(n + s);
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
process("Item", 5);
process(5, "Item");
}
}
Login to try C/C++/Java/PHP code in the editor
Automatic Type Promotion
जब कोई call किसी overload की parameter types से exactly match नहीं करती, Java compile होने में fail होने के बजाय closest compatible version ढूंढने के लिए अपने आप arguments widen करता है (जैसे int को double)।
उदाहरण: Automatic Type Promotion
public class Main {
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(2, 3)); // ints widened to double
}
}
Login to try C/C++/Java/PHP code in the editor
Best Practices in Overloading
Overloads सभी को अलग-अलग inputs के साथ वही underlying operation represent करना चाहिए -- genuinely unrelated behaviors के लिए वही method name उपयोग करना purpose को हरा देता है और API को सही से उपयोग करना confusing बनाता है।
उदाहरण: Best Practices in Overloading
public class Main {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b; // same operation, different input types
}
public static void main(String[] args) {
System.out.println(add(2, 3));
System.out.println(add(2.0, 3.0));
}
}
Login to try C/C++/Java/PHP code in the editor
- सिर्फ return type बदलकर overload करने की कोशिश करना, जो एक compile error है।
- Automatic type promotion के कारण एक surprise call पाना, जैसे
print(5)intयाlongचुनना। - Ambiguous overloads बनाना, जैसे
f(int, double)औरf(double, int)कोf(1, 2)से call करना, जो compile नहीं होता।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: