Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1
Method overloading in Java is a feature that allows a class to have more than one
method with the same name, but different parameter lists.
The compiler differentiates these methods by looking at the number and type of parameters. In the example above, the method add is overloaded in three ways:
The first add method takes two int parameters.
The second add method takes three int parameters. The third add method takes two double parameters. class MathOperations { // Method to add two integers public int add(int a, int b) { return a + b; }
// Overloaded method to add three integers
public int add(int a, int b, int c) { return a + b + c; }
// Overloaded method to add two double values
public double add(double a, double b) { return a + b; } }
public class Main {
public static void main(String[] args) { MathOperations obj = new MathOperations();
// Calling different overloaded add methods
System.out.println("Sum of two integers: " + obj.add(10, 20)); System.out.println("Sum of three integers: " + obj.add(10, 20, 30)); System.out.println("Sum of two doubles: " + obj.add(5.5, 3.7)); } }