Method Overloading in Java
When a class has multiple methods having same name but different in parameters known as Method Overloading.Advantage
- It increases the readability of the program.
Different ways to overload the method
- By changing no. of arguments.
- By changing the data type.
Example -
class Ad
{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class Overloading{
public static void main(String[] args)
{
System.out.println(Ad.add(1,22));
System.out.println(Ad.add(1,22,31));
}
}
Output
23
54
0 Comments