OneCompiler

Method overloading in java

1780

Example heading with h2 size

Example heading with h3 size

Following is sample java code.import java.util.*;

class Calculator {

// Method to add two integers
public int add(int a, int b) {
    return a + b;
}

// Method to add three integers (Overloading with different number of parameters)
public int add(int a, int b, int c) {
    return a + b + c;
}

// Method to add two double values (Overloading with different data types)
public int add(int a, int b, int c ,int d) {
    return a + b +c +d;
}

//public class MethodOverloadingExample {
public static void main(String[] args) {
Calculator calc = new Calculator();

    // Calling the first add method
    System.out.println("Sum of two integers: " + calc.add(10, 20));

    // Calling the second add method
    System.out.println("Sum of three integers: " + calc.add(10, 20, 30));

    // Calling the third add method
    System.out.println("Sum of four integers: " + calc.add(10 20,30,40));
}

}
output
Sum of two integers: 30
Sum of three integers: 60
Sum of four integers: 100