Vegetarians and carnivores are basically the same animals. They have the basic ingredients such as salt and pepper to cook food. They differ in the kind of raw materials they stock to cook their foods and in the way they order food from a chef. Vegetarians and Carnivores can provide the materials to cook but do not know how to cook! In order to get any cooked meal, they have to ask a chef to cook for them. We model them as two concrete subclasses of an abstract class called AEater. AEater has two concrete methods, getSalt and getPepper, and an abstract method called order, as shown in the table below.
public abstract class AEater {
public String getSalt() { }
return "salt"; } public String getPepper() { return "pepper"; } /** * Orders n portions of appropriate food from restaurant r. */ public abstract String order(IChef r, Integer n); // NO CODE BODY! |
public class Vegetarian extends AEater{
public String getBroccoli() { }return "broccoli"; } public String getCorn() { return "corn"; } public String order(IChef c, Object n) { // code to be discussed later; } |
public class Carnivore extends AEater{
public String getMeat() {
return "steak";
} }
public String getChicken() { return "cornish hen"; } public String getDog() { return "polish sausage"; } public String order(IChef c, Object n) { // code to be discussed later; } |
- 1281 reads