0 day streak
java / beginner
Snippet

Method Overloading

Method overloading allows multiple methods in the same class to have the same name but different parameters.

snippet.java
java
1
2
3
4
5
6
7
8
9
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Breakdown
1
int add(int a, int b) {
First version of the method accepting two integers.
2
double add(double a, double b) {
Second version of the method accepting two doubles.