Using Polymorphism

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Dear Valerie,

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 of main() in another class):

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( "Valerie", 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?