simple example of method overloading in java

Method overloading is the simple fundamental of the polymorphism
it method with same name but the number of arguments are different

public class MethodOverLoading {

public static void main(String[] args) {

int Answer1 = Addition(5, 6); // In this case Method 1 will be called
int Answer2 = Addition(5, 6, 7); // In this case Method 2 will be called

System.out.println(“Answer 1 = ” + Answer1);
System.out.println(“Answer 2 = ” + Answer2);
}

// Method 1
public static int Addition(int a, int b) {
System.out.println(“Method 1 is called”);
return a + b;
}

// Method 2
public static int Addition(int a, int b, int c) {
System.out.println(“Method 2 is called”);
return a + b + c;
}
}

output of the example code

Answer 1 = 11
Answer 2 = 18

The above example demonstrate how to implement the Method over loading in java
When we passe the 2 arguments to call the Addition Method Method 1 will be called and
When we passe the 3 arguments to call the Addition Method Method 2 will be called and

Post to Twitter