I have a superclass 'Vehicle' and its subclass 'Car' as shown below. I want to initialise the variable 'wheels' from the constructor of the subclass Car. I can not set the variable via the super() call as it relies on a call to a method.
Is it safe to use the superclass' setter method here? If not is there a problem with the OO fundamentals of the relationship between the classes?
ps. try to excuse the clumsy vehicle/car/chassis example and assume that the method call 'findNumberOfWheels' outside of the constructor is necessary.
Vehicle Class
public abstract class Vehicle {
private int wheels;
private Chassis chassis;
public Vehicle(Chassis chassis){
this.chassis = chassis;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public Chassis getChassis() {
return chassis;
}
public void setChassis(Chassis chassis) {
this.chassis = chassis;
}
}
Car Class
public class Car extends Vehicle{
public Car(Chassis chassis) {
super(chassis);
setWheels(findNumberofWheels(chassis));
}
private int findNumberofWheels(Chassis chassis){
return chassis.getWheels();
}
}