← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 4 of 10

Java में Method Overloading

Method overloading का मतलब है कई methods को वही नाम लेकिन अलग inputs देना, draw जैसा एक word जो आप जो provide करें उसके आधार पर एक circle या एक square draw करने का मतलब हो सकता है। Java सही वाला चुनता है।
Syntax
java
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

java
// 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));
	}
}

Overloading by Parameter Type

आप वही parameter count रखते हुए लेकिन types बदलकर भी overload कर सकते हैं, जैसे add(int, int) बनाम add(double, double), एक method name को naturally कई अलग तरह के input handle करने देते हुए।

उदाहरण: Overloading by Parameter Type

java
// 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));
	}
}

Overloading by Parameter Order

वही types और count रखते हुए भी लेकिन उनका order swap करना, जैसे process(String, int) बनाम process(int, String), compiler के हिसाब से एक distinct, valid overload माना जाता है।

उदाहरण: Overloading by Parameter Order

java
// 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");
	}
}

Automatic Type Promotion

जब कोई call किसी overload की parameter types से exactly match नहीं करती, Java compile होने में fail होने के बजाय closest compatible version ढूंढने के लिए अपने आप arguments widen करता है (जैसे int को double)।

उदाहरण: Automatic Type Promotion

java
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
	}
}

Best Practices in Overloading

Overloads सभी को अलग-अलग inputs के साथ वही underlying operation represent करना चाहिए -- genuinely unrelated behaviors के लिए वही method name उपयोग करना purpose को हरा देता है और API को सही से उपयोग करना confusing बनाता है।

उदाहरण: Best Practices in Overloading

java
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));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. सिर्फ return type बदलकर overload करने की कोशिश करना, जो एक compile error है।
  2. Automatic type promotion के कारण एक surprise call पाना, जैसे print(5) int या long चुनना।
  3. Ambiguous overloads बनाना, जैसे f(int, double) और f(double, int) को f(1, 2) से call करना, जो compile नहीं होता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.