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
- Same method name, different parameters
- Compiler chooses based on arguments
- Return types can be same or different
Related OOP Basics exercises
- Practice Getters and Setters in Java
- Practice Static Members in Java
- Practice Using 'this' Keyword in Java
Practice all OOP Basics exercises · Run this idea in the Java compiler