Method Overloading

 Method Overloading



Method Overloading: If a class has multiple methods with the same name but different parameters, it is known as Method Overloading. These methods are called overloaded methods.

Here's a simple example:
public class MethodOverloading {
// Method to add two integers
int add(int a, int b) {
return a + b;
}

// Overloaded method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}

// Overloaded method to add two double values
double add(double a, double b) {
return a + b;
}
}
public class MethodOverloadingMain{
public static void main(String[] args) {

MethodOverloading methodOverloading = new MethodOverloading();

// Calling the add method with two integers
System.out.println("Sum of 2 and 3 (int): " + methodOverloading.add(2, 3));

// Calling the add method with three integers
System.out.println("Sum of 1, 2 and 3 (int): " + methodOverloading.add(1, 2, 3));

// Calling the add method with two double values
System.out.println("Sum of 2.5 and 3.5 (double): " + methodOverloading.add(2.5, 3.5));
}
}






Comments