This site requires JavaScript, please enable it in your browser!
Greenfoot back
xchange
xchange wrote ...

2017/3/14

Can't override superclass variable with new value in subclass

xchange xchange

2017/3/14

#
Dear All, I can not make inheritance work. I constructed a superclass "Vehicle" and two subclasses "Bus" and "Car". The movement is implemented by the "Vehicle" superclass, i.e. steering, speed-up/ slow-down. The subclasses are meant to extent the superclass by providing an image (car or bus) and setting behavior parameters (like acceleration - cars obviously accelarate faster than busses). My code is thus: public abstract class Vehicle extends Artificial { private int iSpeed=0; private int iAcceleration=0; public void act() { move(iSpeed); if (Greenfoot.isKeyDown("left")) { turn(-1); } if (Greenfoot.isKeyDown("right")) { turn(+1); } if (Greenfoot.isKeyDown("up")) { iSpeed=iSpeed+iAcceleration; } if (Greenfoot.isKeyDown("down")) { iSpeed=iSpeed-iAcceleration; } } } public class CAR extends Vehicle { int iAcceleration=2; } public class BUS extends Vehicle { private int iAcceleration=1; public void DisplayRoute() { System.out.println("Berlin-Paris"); } } Why doesn't it work? Is the logic flawed or the implementation? I want to implement as much generic logic in the superclass and let the subclasses fine-tune it. Thank you for your time, --- XC
danpost danpost

2017/3/14

#
Because your 'iAcceleration' and 'iSpeed' fields are 'private', the subclasses do not have direct access to them. Use 'protected' instead of 'private'.
danpost danpost

2017/3/14

#
I do not think you are going to want the actual key values for movement in the Vehicle superclass. If ever you were to have more than one vehicle (like a car and a but) in the world at one time, they will both be controlled by the same keys and move in a choreographed manner.
xchange xchange

2017/3/14

#
Thank you for your reply. danpost> Because your 'iAcceleration' and 'iSpeed' fields are 'private', the subclasses do not have direct access to them. Use 'protected' instead of 'private'. I've tried setting them to 'protected' already but it didn't work either! But -according to my understanding- it shouldn't matter! Car does not try to access Vehicle's variable because Vehicle doesn't exist! Only Car (and / or) Bus exist as special cases of Vehicle.
Super_Hippo Super_Hippo

2017/3/14

#
Set them to protected in Vehicle and do not have another variable with the same name in bus/car, but just set the value in the constructor:
public class Car extends Vehicle
{
    public Car()
    {
        iAcceleration=2;
    }
}
xchange wrote...
But -according to my understanding- it shouldn't matter! Car does not try to access Vehicle's variable because Vehicle doesn't exist! Only Car (and / or) Bus exist as special cases of Vehicle.
You can't create an instance of Vehicle class, because it is abstract, but Car/Bus are still Vehicles (because they are subclasses). You can't access private fields/methods from anywhere except the class itself no matter if it is abstract or not. But I think the main issue is that you are trying to create another variable in the subclasses instead of using the ones in Vehicle as described above (after making them protected).
danpost danpost

2017/3/14

#
Sorry that I only gave half of the fix. I should have continued to closely look through the codes even after realizing the first part of the problem. I pretty much just stopped after looking at the first few lines and seeing that the fields were 'private' with no 'getter' and 'setter' methods to go along with them.
You need to login to post a reply.