No. Valentine val
and Holiday
are siblings. Since they
are not related by inheritance, a reference variable of one type cannot be
used with an object of the other type.
The Card hierarchy is extended by adding two new classes:
A YouthBirthday birthday card for young people.
This card will add the line "How you have grown!" to the usual birthday greeting.
An AdultBirthday birthday card for old people.
This card will add the line "You haven't changed at all!" to the usual birthday greeting.
The class Birthday
is the parent for these two new classes.
The diagram omits the other two classes, but assume that they are still defined.
Each of the new classes would ordinarily inherit the greeting()
method from
Birthday
.
But we want to override that method with a specialized method in each of the
new classes.
Here is the class definition of Birthday
from the previous chapter:
class Birthday extends Card { int age; public Birthday ( String r, int years ) { recipient = r; age = years; } public void greeting() { System.out.println("Dear " + recipient + ",\n"); System.out.println("Happy " + age + "th Birthday\n\n"); } }
Here is a class definition of YouthBirthday
:
Fill in the blanks. Don't do this too quickly. Check the requirements and the hierarchy diagram.