What is output of the program?
class Test{
public void display(int x, double y){
System.out.println(x+y);
}
public double display(int p, double q){
return (p+q);
}
public static void main(String args[]){
Test test = new Test();
test.display(4, 5.0);
System.out.println(test.display(4, 5.0));
}
}
class Test{
public void display(int x, double y){
System.out.println(x+y);
}
public double display(int p, double q){
return (p+q);
}
public static void main(String args[]){
Test test = new Test();
test.display(4, 5.0);
System.out.println(test.display(4, 5.0));
}
}
A. 9.0 9.0
B. 9 9
C. Compilation Error
D. None of these
Answer: Option C
Arise a ambigutity problem because parameter of both function are same then compiler confuse which function called.
Because different return type is not sufficient to overload a method
An overloaded method may or may not have different return types. But return type alone is not sufficient for the compiler to determine which method is to be executed at run time. It is not possible to have a method with same parameters and different return type.
it is working and the correct answer is A.
test.display(4,5.0) faces ambiguity(confusion) problem, whether it access void display method (or) double display method
There is no point of overloading methods based on return types
overloading concept definition clearly says that the parameters based on no. of arguments or data types or order of arguments should be different.But here the
no. of argument or data types or order of argument is same.hence compilation error.
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
There is a duplication of method, in method overloading the return type is not considered. The number of parameter are same, sequence is same and the datatype is same. There occurs a compilation error.
for function overloading return type of the function should also be same
Change in return type is not valid for method overloading..
Change in return type is not valid for method overloading..