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

