Method Overloading in Java With Examples
Methods of the same can be declared in the same class,as long as they have different sets of parameters ( determine by the number ,type and order of the parameters) this is called Method Overloading .
When an overloaded method is called ,the java compiler selects 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 number that perform the same or similar tasks , but on different types or different numbers of arguments . For example ,Math method abs ,min and max are overloaded with for versions each;
- One with two double parameters.
- One with two float parameters.
- One with two int parameters.
- One with two long parameters.
Declaring Overloaded Methods
In our call class Method Overload we included two overloaded versions of method called square on the calcutes the square of an int (and returns an int ) and one that calculates the square of a double (and returns a double ). Although this methods have the same name and similar parameters list and bodies, think of them simply as different methods .It may help to think of method names as "square of double" respectively .When the application begins execution , class method_overload main method creates an object of class method and calls the objects method method_overload to produce the output
Three ways to overload a method
In order to overload a method, the argument lists of the methods must differ in either of these:
1. Number of parameters.
For example: This is a valid case of overloading
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int) add(int, int, int)
2. Data type of parameters.
For example:
For example:
add(int, int) add(int, float)
3. Sequence of Data type of parameters.
For example:
For example:
add(int, float) add(float, int)
#MethodOverload.Java
#Overloaded method declarations .
Example 1:
public void display(){
System.out.println("Square of integer 77 is \n "+ square(77));
System.out.println("Square of double 88.5 is \n "+ square(88.5));
}
public int square(int a){
System.out.println(a);
return a*a;
}
public double square (double d){
System.out.println(d);
return d*d;}
}
public class method_overload {
public static void main(String[] args) {
method obj= new method();
obj.display ();
}
}
Output:
77
Square of integer 77 is
5929
88.5
Square of integer 88.5 is
7832.25
Example 2:
public class mathodoverload {
public static int Add(int a, int b) {
return a+b;
}
public static float Add(float a, float 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;
}
public static void main(String[] args) {
int a = 10, b = 30;
float c = 3.50f, d = 90.4f;
double e = 3.2233332, f = 4.3424551144;
String g="I Love ", h="Java";
System.out.println(Add(a,b));
System.out.println(Add(c,d));
System.out.println(Add(e,f));
System.out.println(Add(g,h));
}
}
Output :
40
93.9
7.5657883144
I Love Java
_______________*******************____________________