I saw the code below online and the subclass constructor calls the superclass constructor before initialising its own variable (i.e. public int seatHeight
), after I change the order of initialisation, i.e. by putting seatHeight = startHeight;
before super(startCadence, startSpeed, startGear);
, my IDE displays error message. I was just wondering what is the reason behind calling superclass constructor before the subclass can initialise its own variable? And what are some of the rules governing superclass and subclass initialisation?
public class MountainBike extends Bicycle {
public int seatHeight;
public MountainBike(int startHeight, int startCadence,
int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);//when change order with seatHeight = startHeight, IDE display error
seatHeight = startHeight;
}
public void setHeight(int newValue) {
seatHeight = newValue;
}
}