Method Overloading in Java
Method Overloading is the term used to denote use of same method name with different parameters. Java supports overloading of methods. For example, three methods in a class could be used to add numbers. These methods could be defined in such a way that if it takes 2 parameters, it would use the method with two parameters and if it takes three parameters, it would use the method with three parameters and so on. The JVM treats methods with same name but different parameters as separate methods.
Two Methods with same name but different parameters
class Addition
{
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(Addition.add(11,11));
System.out.println(Addition.add(11,11,11));
}
}
Output of the Program will look like this:
Download Source code of 'Method Overloading' Program
References : Oracle
share on : :
Love to hear your Views / Guidance / Recommendations on this Post…