I know that it is bad design to have a protected variable in a parent class because all child classes can change that value. However, I was trying to test it out but I am doing something wrong here. It's telling me that cannot find symbol speed = 999999;
in Truck class. I thought the child class has access to the protected variable speed
in parent class.
public class Vehicle {
protected double speed;
protected double maxSpeed;
public Vehicle(double speed, double maxSpeedIn) throws InvalidDataException{
setSpeed(speed);
maxSpeed = maxSpeedIn;
}
public void setSpeed(double s) throws InvalidDataException {
if (s < 0.0) {
throw new InvalidDataException("Negative speed is not valid" );
}
if (s > maxSpeed) {
throw new InvalidDataException("Speed cannot exceed maximum spped:");
}
speed = s;
}
}
public class Truck extends Vehicle {
public Truck(double speedin, double maxSpeedin) throws InvalidDataException {
super(speedin,maxSpeedin);
}
speed = 999999;
}
Your speed = 99999; line isn't valid the way you placed it in the Truck class. Try to put it somewhere else.
You could for example, just for your testing purpose, put it inside Truck's constructor, after the call to super.
Note that you'd have had the exact same error if you had chosen another name altogher, like this:
public Truck extends Vehicle {
public Truck(double speedin, double maxSpeedin) throws InvalidDataException {
super(speedin,maxSpeedin);
}
justTesting = 999999;
}