Understanding Java Method Overloading tutorial




COMPLETE CODES:
public class MethodOverloading{
public static void main(String [] args){
int result = subtract(5,1);
System.out.println("subtract(5,1) =" +result);
result = subtract(5,2,1);
System.out.println("subtract(5,2,1)= " +result);
result = mul(2,4);
System.out.println("mul(2,4)" = +result);
float floatResult = mul(2.4f,3,5f);
System.out.println("mul(2.4f,3.5f)=" +floatResult);
}
private static int subtract(int a, int b){
return a-b;
}
private static int subtract(int a, int b, int c){
return a-b-c;
}
private static int mul(int a, int b){
return a * b;

}
private static float mul(float a, float b){
return a * b;
}
}


In this article you will learn what is overloading methods and how to overload method, Overloaded methods are methods that have the same name but different type parameter, the only part that is different in those methods are parameters but the name is same, Overloading also allows a different number of parameters, Overloaded methods can also have different return types and different access to its type. Let's say we need to subtract two numbers and we need to subtract three numbers, So creating a method, Subtract with two parameters like private static int subtract(int a, int b){return a-b;} and we will just 'return a-b;', Now if we want to create another method that will subtract three numbers like private static int subtract(int a, int b, int c){return a-b-c;} this will compile and we can 'return a-b-c', You can see that the method has the same name it has the same 'return type' but different numbers of parameters, Calling a method with numbers for example "subtract2", 'subtract3' doesn't make much sense, but it is easier to overload that method and to specify different types of arguments or a different number of arguments. So let's try those methods into int result = subtract(int a, int b); Notice how we have two methods with the same name that autocomplete offers us in IDE Eclipse or IntelliJ, so we have subtracted with two parameters and subtract with three parameters. Method overloading is used everywhere even in java. For example, let's say we have int result = subtract(5,1); When we print this out by System.out.println("subtract(5,1)="+result); And also we want to call the method with three parameters so we can again change our result variable like result = subtract(5,2,1); and in this way we are calling a different method, Again we can print the result like System.out.println("subtract(5,2,1)="+result); then let's run the code and see that both methods are working properly, We can also have a different type of parameters in a different return type, Let's say we want to have a method that multiplicates integer number and the method that multiply float numbers like e.g private static int mul(int a, int b){return a*b;} and we will return a*b, But we also want to be able to multiply the decimal numbers or float numbers like private static float mul(float a float b){return a * b;} float are decimal numbers, as we can see that the Method (mul) is overloaded with float and with an integer, So we can call any of these methods depending on the type of parameters so we can say result = mul(2,4), and you will notice how IDE offers us two different methods and for this time we specify mul(2,4); since we want to multiply 2 & 4 Now we can again print the result by System.out.println("mul(2,4)=" +result); if we want to use a float method if we type result = mul(2.4f, 3.5f); if we don't specify "f" letter then by default it will be "double"since in java all decimal number are double. And this time we will get compilation error since we are trying to assign a float value to an int variable, So we will declare another variable which is 'float floatResult = mul(2.4f,3.5f); And then we can print the float result output with System.out.println("mul(2.4f,3.5f)= " +floatResult); When we run the code will compile and we can use the same method with the same name just with different parameters. And that is method overload if you notice multiplication 2 * 4 = 8 and 2.4f * 3.5f = 8.75 so that is method overloading sample.     




Post a Comment

0 Comments