I believe this is a rather easy question to answer, and is probably a repost.
Thing is, I don't exactly know what to search for. I've tried searching for generic terms regarding overloading, but I can only find examples of different ways to use different types or number of arguments.
I think it's easier to understand the problem with an example:
Let's suppose I have an Adder
class, which allows me to add two integers, but I want to be able to call it with only one integer, using 1
as the second integer:
public class Adder {
int sum;
public static void main(String[] args) {
Adder myAdder = new Adder(3);
myAdder.printSum();
}
public Adder(int a, int b) {
this.sum = a + b;
}
public Adder(int a) { // Overloading for default behaviour
return Adder(a, 1);
}
public printSum(){
System.out.println(this.sum);
}
}
As you can see, I'm trying to add a new constructor for a single integer, where it just calls Adder(a, 1)
instead, but constructors can't return anything, as far as I'm concerned.
What am I missing to do this?