METHOD OVERLOADING IN JAVA
Method Overloading in Java
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
There are two ways to overload the method in java
- By changing number of arguments
- By changing the data type
Why Method Overloading is not possible by changing the return type of method only?
In java, method overloading is not possible by changing the return type of the method only because of ambiguity.
class Adder{
static int add(int a,int b)
{return a+b;}
static double add(int a,int b)
{return a+b;}
}
class Test{
public static void main(String[] args){
System.out.println(Adder.add(10,10));//ambiguity
}}
Can we overload java main() method?
Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only.

Comments
Post a Comment