I'm following the below UML to create the classes that call delegate methods from an engine class, but when I tired to create an object of the Ferrari class and pass it the type of engine, ie a CombustionEngine
.
I get the error CombustionEngine cannot be resolved to a variable
.
I gather from this that the enzo
object can't see the CombustionEngine
class that is extended from the Engine
interface.
Can anyone see where I have gone wrong with the implementation of the hierarchy?
The CombustionEngine
class extended from Engine interface
:
public class CombustionEngine implements Engine {
//instance variables
private int speed;
public CombustionEngine(int speed) {
super();
this.speed = speed;
}
public CombustionEngine() {
super();
// TODO Auto-generated constructor stub
}
public void setSpeed(int speed) {
this.speed = speed;
}
@Override
public void stop() {
// TODO Auto-generated method stub
System.out.println("Combustion engine Stopped:");
}
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("Combustion engine Startedd:");
}
@Override
public void throttle(int power) {
// TODO Auto-generated method stub
speed += power;
}
@Override
public int getSpeed() {
// TODO Auto-generated method stub
return speed;
}
}
The Engine
interface:
public interface Engine {
void stop();
void start();
void throttle(int power);
int getSpeed();
}
PowerVehicle
class:
public class PoweredVehicle {
Engine engine;
public PoweredVehicle(Engine engine) {
super();
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
public void Drive(){
System.out.println("Generic Driving");
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public void stop() {
engine.stop();
}
public void start() {
engine.start();
}
public void throttle(int power) {
engine.throttle(power);
}
public int getSpeed() {
return engine.getSpeed();
}
}
The Ferrari
class:
public class Ferrari extends PoweredVehicle {
public Ferrari(Engine engine) {
super(engine);
// TODO Auto-generated constructor stub
}
Ferrari f = new Ferrari(engine);
@Override
public void Drive() {
// TODO Auto-generated method stub
super.Drive();
System.out.println("Ferrari driving...");
}
}
Finally in the Runner
class I try to create an instance of Ferrari
public class Runner {
public Runner() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Ferrari enzo = new Ferrari(CombustionEngine);
}
}
Why are you creating an instance of Ferrari
inside the Ferrari
class?
public class Ferrari extends PoweredVehicle
{
public Ferrari(Engine engine) {
super(engine);
}
Ferrari f = new Ferrari(engine); // Why would you do this here?
@Override
public void Drive() {
super.Drive();
System.out.println("Ferrari driving...");
}
}
And you have to pass in an instance of CombustionEngine, when creating a Ferrari
object
public static void main(String[] args)
{
int someSpeed = 99;
Ferrari enzo = new Ferrari(new CombustionEngine(someSpeed));
}
Here, you are implementing an interface, so there should not be a need for super()
public class CombustionEngine implements Engine
{
//instance variables
private int speed;
public CombustionEngine(int speed)
{
//super();
this.speed = speed;
}
....
}