-1

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()); } } 
1
  • 3
    AbstractCalculator(a, 0) replace with this(a, 0); Commented Oct 18, 2019 at 11:17

1 Answer 1

2
// existing functionality public AbstractCalculator(int a){ this(a, 0); } 
Sign up to request clarification or add additional context in comments.

1 Comment

yep thats it - thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.