Page 1281 - AP Computer Science A, 7th edition
P. 1281
4. (a) public class SnowyOwl extends Owl {
//Constructor
public SnowyOwl()
{ super (“Snowy owl”); }
//Returns type of food for this SnowyOwl public String getFood()
{
int num = (int)(Math.random()∗3); if (num == 0)
return “hare”; else if (num == 1)
return “lemming”; else
return “small bird”;
} }
(b) public void allEat() {
for (Bird b: birdList) System.out.println(b.getName() + “ ” + b.getFood());
}
NOTE
• The Owl class inherits the abstract getFood method. Since the food type for an Owl depends on the type of Owl, the Owl class does not provide implementation code for getFood. This is the reason that the Owl class is an abstract class.
• In part (a), since SnowyOwl is a concrete (nonabstract) class, it must provide implementation code for getFood.
• In part (a), super must be used in the constructor because there is no direct access to the private instance variables of