go to previous page   go to home page   go to next page

Answer:

Dear Sophia,

Happy 7th Birthday


How you have grown!!

Using Polymorphism

Assume that AdultBirthday has been defined. It will look much like YouthBirthday but with a slightly different birthday message. Here is a program fragment (part a main()):

AdultBirthday ab = new AdultBirthday( "Walter", 47 );
ab.greeting();

It will write out:

Dear Walter,

Happy 47th Birthday


You haven't changed at all!

Now inspect the following code:

Card crd = new YouthBirthday( "Sophia", 7 );
crd.greeting();

crd      = new AdultBirthday( "Walter", 47 );
crd.greeting();

crd      = new Birthday( "Zoe", 30 );
crd.greeting();

QUESTION 9:

Is this code correct? Can crd be used for each of the three objects?