Method Overloading in Java: Explanation & Practice

Create overloaded methods

Problem summary

Create a Calculator class with overloaded add methods for 2 and 3 numbers.

Starter code

// Create Calculator with:
// - int add(int a, int b)
// - int add(int a, int b, int c)

public class Main {
    public static void main(String[] args) {
        // Test both add methods
        // Print: Sum of 2: 15
        // Print: Sum of 3: 30
    }
}

Expected output and test cases

  • Overloaded methods
    Sum of 2: 15
    Sum of 3: 30

Hints

  1. Same method name, different parameters
  2. Compiler chooses based on arguments
  3. Return types can be same or different

Related OOP Basics exercises

Practice all OOP Basics exercises · Run this idea in the Java compiler