Complete the PirateParrot practice problem before reviewing the solution.
Review the PirateParrot
solution with AP CS Tutor Brandon Horn.
public class PirateParrot extends Parrot
{
/* The total number of years the pirate parrot has stolen */
private int yearsStolen;
public PirateParrot(String name)
{
super(name);
train("Polly want a cracker");
yearsStolen = 0;
}
public int getAge()
{
return super.getAge() + yearsStolen;
}
public void stealSoul(int soulAge)
{
yearsStolen += soulAge;
}
}
A PirateParrot
is a Parrot
so PirateParrot extends Parrot
.
A subclass contains functionality that is new or different from that in its superclass.
Subclasses do not inherit superclass constructors. PirateParrot
must explicitly declare a constructor that accepts a name. The PirateParrot
constructor must explicitly call the Parrot
constructor shown and pass it the name. The super
call must be the first line.
The stealSoul
method is new.
The getAge
method is different than the Parrot getAge
method. The age of a PirateParrot
is computed differently than the age of a Parrot
. The PirateParrot getAge
method overrides the Parrot getAge
method.
The instance variable yearsStolen
is new. There is no such thing as an instance variable that is different. yearsStolen
stores only the years that the PirateParrot
stole. The Parrot
class already stores its natural age as a Parrot
.
The Parrot speak
and Parrot train
methods should not be overriden since a PirateParrot
does not speak or train differently than a Parrot
. Parrot
already stores the list of sounds that can be made and chooses one at random when speaking. The PirateParrot
constructor runs the Parrot train
method to teach the PirateParrot
the required sound.