write a program method overloading in java
Sat Jul 06 2024 06:03:06 GMT+0000 (Coordinated Universal Time)
Saved by
@projectrock
public class MathOperations {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two doubles
public double add(double a, double b) {
return a + b;
}
// Method to concatenate two strings
public String add(String a, String b) {
return a + b;
}
public static void main(String[] args) {
MathOperations math = new MathOperations();
// Test the overloaded methods
System.out.println("Addition of two integers: " + math.add(10, 20));
System.out.println("Addition of three integers: " + math.add(10, 20, 30));
System.out.println("Addition of two doubles: " + math.add(10.5, 20.5));
System.out.println("Concatenation of two strings: " + math.add("Hello", " World"));
}
}
content_copyCOPY
Comments