Alex Walton Alex Walton - 10 months ago 64
Java Question

multiple Constructors and if Statement

I want to, within my abstract

, define two constructors.

When create a new instance of the
, i want the toString to return something different depending on what was called:

The FireEngine

public class FireEngine extends EmergencyVehicle {

private String colour;

public FireEngine(String colour) {
super (colour);

public FireEngine() {


public String toString () {
if (colour == "red") {
return "red";
} else
return "no";


public abstract class EmergencyVehicle extends RoadVehicle {

public boolean codeBlue = false;

public EmergencyVehicle(String colour){

public boolean isEmergency () {
if (codeBlue == true) {
return true;
} else {
return false;

public void setEmergency(boolean newEmergency) {
codeBlue = newEmergency;


This is a homework exercise so I don't want the answer per se, but does the above code make sense?

For example, if I add a new EmergencyVehicle, I want an if statement depending on what colour the vehicle I add is.

Answer Source

1st Remark

Don't call


in the default constructor, do

colour = "red";

unless the EmergencyVehicle(String colour) RoadVehicle(String colour) constructor is doing something else.

2nd Remark

Don't compare using

if (colour == "red")


if ("red".equals(colour))

3rd Remark

The method

public String toString()

is supposed to return a string representation of the instance. You implementation only returns red or no which is not very informative. Use something like

return("FireEngine(colour=" + colour + ")");