Can method overloaded based on different return but same argument type

Can method overloaded based on different return but same argument type

Answer : No In java method overloading done based on their parameter.

It’s possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. If this is the case the methods are said overloaded, and process is called as method overloading. Through Method overloading is also one way where Java supports polymorphism.

Below is sample example that shows method overloading:

public class MethodOverloadingTest {

	public static void main(String[] args) {
		MethodOverloadingTest methodOverload = 
				new MethodOverloadingTest();
		double result;
		methodOverload.method();
		methodOverload.method(10);
		methodOverload.method(10, 20);
		result = methodOverload.method(15.55);
		System.out.println("Result of ob.method(15.55): " + result);

	}

	void method() {
		System.out.println("No parameters");
	}

	// Overload method for one integer parameter.
	void method(int a) {
		System.out.println("a: " + a);
	}

	// Overload method for two integer parameters.
	void method(int a, int b) {
		System.out.println("a and b: " + a + " " + b);
	}

	// overload method for a double parameter
	double method(double a) {
		System.out.println("double a: " + a);
		return a * a;
	}
}

 

Output:

Can method overloaded based on different return but same argument type

Leave a Reply

Your email address will not be published. Required fields are marked *