Why do you want to chain constructors ?
As you chain constructors, you have to specify some default values from the constructor with less argument to the invoked constructor with more arguments.
You do the reverse here :
public Flight(int passengers, int seats, double maxKgPerPassenger){ this(passengers); this(seats); this(maxKgPerPassenger); }
You indeed want to invoke from the constructor with the most argument all other constructors.
It makes no sense and it is not valid either as a constructor can invoke a single other constructor of the same class.
I want to chain to all constructors so that they can all be private other than the main constructor.
Things work in the reverse order.
The actual code doesn't seem to need constructor chaining.
This one is enough :
public Flight(int passengers, int seats, double maxKgPerPassenger){ this.passengers = passengers; this.seats = seats; this.maxKgPerPassenger = maxKgPerPassenger; }
Suppose that class clients could create Flight instances with two flavors :
- by passing all parameters
- by passing only the
seats parameter (remains being valued with default values)
You could so write :
public Flight(int passengers, int seats, double maxKgPerPassenger){ this.passengers = passengers; this.seats = seats; this.maxKgPerPassenger = maxKgPerPassenger; } public Flight(int seats){ super(50, seats, 10); // you pass some default values for passengers and maxKgPerPassenger }
private Flight(int seats){ this(20,seats, 20.0D); }