calling method from extended Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Masters1989
    New Member
    • Nov 2016
    • 1

    calling method from extended Classes

    I am basically using Java Constructor chaining, to try and add vehicles to my TrafficQueue. If I run the below code:



    The part of code I am struggling to get working is
    Code:
     
    
        public void add(RoadVehicle vehicle) {
            if (isFull()) {
                System.out.println("Queue is full! We cannot add any more vehicles to the Queue!");
                System.exit(1);
            } else {
    
    
                if (vehicle instanceof EmergencyVehicle && vehicle.isEmergency())  {
    
                    addToFront(vehicle);
                } else {
                    addToBack(vehicle);
                }
    
            }
        }
    It wont allow me to run the method isEmergency() on vehicle.


    FireEngine class:
    Code:
    public abstract class  EmergencyVehicle extends RoadVehicle {
    	
    	
    	public boolean codeBlue = true;
    	
    	
    	
    	public EmergencyVehicle(String colour){
    		super(colour);
    	}
    	
    	
    	
    	
    	
    	public boolean isEmergency() {
    		return codeBlue;
    	}
    	
    	public void setEmergency(boolean newEmergency) {
    		codeBlue = newEmergency;
    	}
    	
    }
    and EmergencyVehicl e class:

    Code:
    public class FireEngine extends EmergencyVehicle {
    
    
        public FireEngine(String colour) {
    		super (colour);
    		
    	}
    	public FireEngine() {
    		this("red");
    		
    	}
    
    
    	public String toString () {
    		if ("red".equals(colour)) {
    
    			return "Fire Engine : CODEBLUE";
    
    
       	}   else
    			return colour + " FireEngine.";
    	}
    }
    So, using extends, I am trying to add the vehicle to the front of the queue, if it isEmergency returns true.

    The error I get is:
    error, cannot find symbol.

    I don't need the exact solution, but I think it should work as I have got the extends in each class. Can anyone point out where it goes wrong? I realise that the code needs neatening up slighty!

    When adding a vehicle to the queue, I use

    Code:
    FireEngine car1 = new FireEngine("red");
Working...