If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
If we have to perform only one operation, having same name of the methods increases the readability of the program.
Example:-
Public Class MyClass
{
public static void main(string[] args)
{
System.out.println Add(1,36);
System.out.println Add(5.65,40.65);
System.out.println Add(“Hello”,”World”);
}
public static int Add(int a,int b)
{
return(a+b);
}
public static double Add(double a,double b)
{
return(a+b);
}
public static string Add(string a,string b)
{
return(a+b);
}
}
If we have to perform only one operation, having same name of the methods increases the readability of the program.
Example:-
Public Class MyClass
{
public static void main(string[] args)
{
System.out.println Add(1,36);
System.out.println Add(5.65,40.65);
System.out.println Add(“Hello”,”World”);
}
public static int Add(int a,int b)
{
return(a+b);
}
public static double Add(double a,double b)
{
return(a+b);
}
public static string Add(string a,string b)
{
return(a+b);
}
}
0 Comments:
Post a Comment