In this section we will talk about the argument lists in method overloading. Method overloading can be achieved if method signature is different. It can be different number of parameters, data types or sequence but return type doesn't matter. In our example we created a program for some simple calculation.
method 2: three parameters (three int)
method 3: two parameters (one int, one float)
method 4: two parameters (one int, one float) but in different sequence
class A
{
public static int method1(int x, int y) {return x+y;}
public static int method2(int x, int y, int z) {return x+y+z;}
public static float method3(int x, float y) {return y-x;}
public static float method4(float x, int y) {return x-y;}
}
public class OverloadingTest1
{
public static void main(String args[])
{
System.out.println("method 1: "+A.method1(5,9));
System.out.println("method 2: "+A.method2(4,7,2));
System.out.println("method 3: "+A.method3(5,14.2f));
System.out.println("method 4: "+A.method4(7.8f,2));
}
}
Result:
method 1: 14
method 2: 13
method 3: 9.2
method 4: 5.8
No comments:
Post a Comment