An array of Card:

Card[] cards = new Card[12];

Array of Related Objects

Now any object that belongs in the hierarchy can fit into any slot of the array. This is like the greeting card display at the drug store that holds a different selection of cards in different seasons of the year. Here is a program snippet:

Card[] cards = new Card[12];

cards[0]     = new YouthBirthday( "Valerie", 7 );
cards[1]     = new AdultBirthday( "Walter", 47 );
cards[2]     = new Birthday( "Zoe", 30 );
cards[3]     = new Holiday( "Kelly" );
cards[4]     = new Valentine( "Jill", 42 );

for ( int j=0; j <= 4; j++ )

This code will create 5 cards objects of various varieties and put them into the array. Then it will ask each object in the array to write out its own version of the greeting. This will work fine:

Dear Valerie,
Happy 7th Birthday
How you have grown!!

Dear Walter,
Happy 47th Birthday
You haven't changed at all!

Dear Zoe,
Happy 30th Birthday

Dear Kelly,
Season's Greetings!

Dear Jill,
Love and Kisses,

(I've eliminated some blank lines from the actual output.)


Now say that this code follows the above:

Card temp;

temp     = cards[0];
cards[0] = cards[1];
cards[1] = temp;