Method Overloading
Method Overloading :
Explanation :
You can define multiple methods in a class with the same name but different parameters. The appropriate method is chosen based on the arguments provided.
Example:
class MathOperations
{
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperations math = new
MathOperations();
System.out.println(math.add(5, 7)); // Output: 12
System.out.println(math.add(3.5, 2.5));
// Output: 6.0
}
}
Comments
Post a Comment