Java

OOP GUIDE / WORK

 

FOOD SUPERCLASS 2

 

Topics

  • Using protected instance variables
  • Using extends and super()
  • Overriding toString()

 

TASK – PART 1 – SETUP – FOOD CLASS

 

To start, you need the Food class from the first part of this work.  You can either go do that work or go get it from the Solutions.

 

 

TASK – PART 2 – SANDWHICH CLASS

 

We will create the Sandwhich class with the following specifications:

 

·       It will extend the Food class.

·       It has the following instance variables (all of type String):

o breadType,

o toppings, and

o spreads

·       It has a constructor that gets a value for all 7 instance variables.  Remember that three of the instance variables are inherited from Food.

·       It has a toString() method that returns something similar to:
                   name on breadType bread


Note: To access the name instance variable, it needs to be set to protected instead of private.

 

TASK – PART 3 – SANDWHICH TESTER

Create in SandwhichTester class, create a few Sanwhich objects and test your toString() method.

Create a Food array of 5 elements called lunch. 

 

Place at least two Food elements and two Sandwhich elements into the array.

 

Loop over the lunch array to display its content.

 

Note: The toString method in the Food class should maybe be improved to include the name of the food and its flavour.