I am extending the API of a class that I am working on and would like to make it backward compatible - so that it doesn't break any existing users of the class. I have created the new functionality by adding a new parameter to the constructor.
So that I don't repeat code (I could copy-paste code from the old constructor into the new) I would like to change the existing constructor to call the new constructor passing 0 as the default for the new parameter but I am getting a compilation error, method call expected
.
Is what I am trying to do not possible (method overloading is not supported?) or am missing something?
To help show what I mean, please see the simple example below,
public abstract class AbstractCalculator {
int result;
// new parameter being added
public AbstractCalculator(int a, int b){
result = a + b;
}
// existing functionality
public AbstractCalculator(int a){
//compilation error here - Method call expected
AbstractCalculator(a, 0);
}
}
public class Calculator extends AbstractCalculator{
public Calculator(int a){
super(a);
}
public static void main(String args []){
Calculator calc = new Calculator(4);
System.out.println(calc.getResult());
}
}