This is not true
Explanation:
Method overloading allows a class to have more than one method with the same name but different types or different numbers of parameters.
If two methods have the same name, same number of parameters, and same type of parameters but return type is different then this is not method overloading but it will display syntax error that the method is already defined.
For example:
public class Main
{
//method to add two numbers with return type int
public static int add(int a, int b)
{
return a+b;
}
//method to add two numbers with return type float
public static float add(int a, int b)
{
return a+b;
}
public static void main(String[] args)
{
//method calling and display result
System.out.println(add(10,
20));
}
}
Error: method add(int,int) is already defined in class.
The above program will through error because only the return type is different for the 'add' method.
So, in method overloading, the return type of the method is not included.
Get Answers For Free
Most questions answered within 1 hours.