3

Hi. I am trying to call a method from a superclass via a subclass but keep getting an error. The method I am trying to call is setDestination() however the compiler keeps giving me the error "cannot find symbol - method setDestination(java.lang.string)" my lecturer says that this is a simple miss match of the parameters of the method call and the method, but I have both method parameters as of type String so I am a little confused.

My code is:

Superclass Vehicle:

     public class Vehicle
     {
     // A unique ID for this vehicle
     private String id; 
     // The next destination of this Vehicle.
     private String destination;

     /**
     * Constructor for objects of class Vehicle
     */
     public Vehicle(String id)
     {
         destination = null;
     }

     /**
     * Return the ID of the Vehicle.
     * @return The ID of the Vehicle.
     */
     public String getID()
     {
        return id;
     }

     /**
     * Return the destination of the Vehicle.
     * @return The destination of the Vehicle.
     */
     public String getDestination()
     {
         return destination;
     }

     /**
     * Set the intented destination of the Vehicle.
     * @param destination The intended destination.
     */
     private void setDestination(String destination)
     {
         this.destination = destination;
     }
}

subclass Taxi:

public class Taxi extends Vehicle
{
    // The location of this taxi.
    private String location;
    // Whether it is free or not.
    private boolean free;

   /** 
    * Constructor for objects of class Taxi.
    * @param base The name of the company's base.
    * @param id This taxi's unique id.
    */
    public Taxi(String base, String id)
    {
         super(id);
         location = base;
         free = true;
    }

   /**
    * Book this taxi to the given destination.
    * The status of the taxi will no longer be free.
    * @param destination The taxi's destination.
    */
    public void book(String destination)
    {
        setDestination(destination);
        free = false;
    }

   /**
    * Return the status of this taxi.
    * @return The status.
    */
    public String getStatus()
    {   
        return getID() + " at " + location + " headed for " +
        destination;
    }

    /**
     * Return the location of the taxi.
     * @return The location of the taxi.
     */
     public String getLocation()
     {
         return location;
     }

     /**
     * Indicate that this taxi has arrived at its destination.
     * As a result, it will be free.
     */
     public void arrived()
     {
         location = destination;
         destination = null;
         free = true;
     }
 }

Any help much appreciated.

GareginSargsyan
  • 1,877
  • 15
  • 23
Simon
  • 33
  • 4

5 Answers5

7

It's a private method. You cannot access private methods. Change it to protected.

You can access private members with in the class only.

   Modifier Class   Package Subclass    World
    ---------------------------------------------
    public      Y      Y        Y           Y
    protected   Y      Y        Y           N
    no modifier Y      Y        N           N
    private     Y      N        N           N

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
4

You have defined your setDestination as a private

private void setDestination(String destination) 

So you cannot access it via inheritance. Either change it to protected or `public. Learn more info on Access modifiers here.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
2

private Modifier can't access from outside of class.

Sanjay Bhimani
  • 1,593
  • 1
  • 15
  • 29
1

The private methods are not inherited , a superclass reference calls its own private method.

use private void setDestination(String destination) instead of

 public void setDestination(String destination) 
Sitansu
  • 3,225
  • 8
  • 34
  • 61
0

Additionally to the previous answers, the destination class member is declared private. As such it will not be accessible to any subclass if called directly (as in arrived() and getStatus() methods of Taxi class).

Either declare the destination as protected or change modifier of getDestination() to public or protected and use the method instead of direct access of the class member - as others mentioned earlier.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
mishatim
  • 1
  • 1