New Definition of YouthBirthday

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.

Is this code correct?

YouthBirthday birth;
birth = new Birthday( "Terry", 23 );

Answer:

No. A reference variable for a child class (YouthBirthday) cannot be used for an object of a parent class (Birthday).

New Definition of YouthBirthday

Parents can accommodate children, but children can't accommodate parents. If you find this rule hard to remember, just think of yourself and your parents:

It is OK for you to go home and stay in your parent's house for a few days, but it's not OK for them to stay in your dorm room for a few days.

Now let us re-write YouthBirthday in order to show more about polymorphism. Say that you want two greeting() methods for YouthBirthday:

  1. The parent's greeting() method.
    • This will be included by inheritance.
  2. A method with the name of the sender as a parameter.
    • This will be an additional method―it will not override the parent's method.
    • In addition to what the parent's method does, this method will write out "How you have grown!! Love," followed by the name of the sender.

Here is a skeleton:

// Revised version
//
class YouthBirthday  extends Birthday

{
  public  YouthBirthday ( String r, int years )
  {
    super ( r, years )
  }

  // additional method---does not override parent's method
  //
  public void greeting(    )
  {
    super.greeting();
    System.out.println("How you have grown!!/n");
    
    System.out.println("Love, " +   + "/n" );
  }
}

QUESTION 11:

Fill in the missing parts.