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
Join The Discussion
Comments ( 12 )
What is method overriding in Java?
A. Redefining a superclass method in a subclass
B. Defining a new method with the same name in a subclass
C. Making a method private in a subclass
D. Hiding methods in a superclass
What is the purpose of method overloading in Java?
A. Creating static methods
B. Hiding methods in a superclass
C. Redefining methods in a subclass
D. Defining multiple methods with the same name but different parameters
A. @OverrideMethod
B. @OverrideSuper
C. @Override
D. @OverrideParent
What happens when a subclass tries to override a final method from the superclass in Java?
A. The final method is hidden
B. It results in a compilation error
C. The final method becomes static
D. The final method is hidden
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..