go to previous page   go to home page   go to next page highlighting

Answer:

Recall that all child classes have a toString() method which they inherit from the class Object unless the child class overrides it (as is done here.)


Starting the Program

Here is a class definition for Goods. The two instance variables are made protected so that children classes can use them.

class Goods
{
  protected String description;
  protected double price;

  public Goods( String des, double pr )
  {
    description = des;
    price       = pr;
  }

  public String toString()
  {
    return "item: " + description +  " price: " + price ;
  }
}

Here is a skeleton of the class Food, a child of Goods. It adds the variable calories.

class Food 
{
  protected double calories;

  public Food( String des, double pr, double cal)
  {
    super( , );
    
    calories =  ;
  }

  public String toString()
  {
    return super. + calories + " calories";
  }
}

QUESTION 8:

Fill in the blanks. Click here for a


go to previous page   go to home page   go to next page