Java - Method Overloading

Method Overloading


Method are the same name can be declared in the same class,as long as they have different sets of parameters is called Method overloading.When the is called, the java compiler the appropriate method by examining the number, types and order of the arguments in the call. Method overloading is commonly used to create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments.

Method overloading is the feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in java, that allows a class to have more than one constructor having different argument list.


Three ways to overload a Method:
1) Number of Parameters:
     Eg: This is valid case.
            add(int,int)
            add(int,int,int)
2) Data type of Parameters:
      Eg: This is valid case.
            add(int,int)
            add(int,float)
2) Sequence of Data type of Parameters:
      Eg: This is valid case.
            add(int,float)
            add(float,int)

What is the advantage?
We don’t have to create and remember different names for functions doing the same thing. For example, in our code, if overloading was not supported by Java, we would have to create method names like sum1, sum2, … or sum2Int, sum3Int, … etc.

Example program for Method overloading:

// Java program to overloading in Java. 

public class Sum { 

// Overloaded sum(). This sum takes two int parameters 
public int sum(int x, int y) 
return (x + y); 

// Overloaded sum(). This sum takes three int parameters 
public int sum(int x, int y, int z) 
return (x + y + z); 

// Overloaded sum(). This sum takes two double parameters 
public double sum(double x, double y) 
return (x + y); 

public static void main(String args[]) 
Sum s = new Sum(); 
System.out.println(s.sum(10, 20)); 
System.out.println(s.sum(10, 20, 30)); 
System.out.println(s.sum(10.5, 20.5)); 


Can we overload static methods?
The answer is ‘Yes’. We can have two ore more static methods with same name, but differences in input parameters. For example, consider the following Java program. Refer this for details.
Can we overload methods that differ only by static keyword?
We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same). See following Java program for example. Refer this for details.
Can we overload main() in Java?
Like other static methods, we can Overload Method in Java. Refer overloading main() in Java for more details.
Example program for Overloading a Main Method:

public class egformain {

// Normal main()
public static void main(String[] args)
{
System.out.println("Overload a Main Method");

}

// Overloaded main methods
public static void main(String arg1)
{
System.out.println("Example for " + arg1);

}
public static void main(String arg1, String arg2)
{
System.out.println( arg1 + " " + arg2);
}
}



Output:

Example for Overload a Main Method.


Comments

Post a Comment

Popular Posts