Extended Hierarchy

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:

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.

Extended Hierarchy

extended card hierarchy

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:

class    extends   

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

  public void greeting()
  {
     ();
    System.out.println("How you have grown!!/n");
  }
}

QUESTION 7:

Fill in the blanks. Don't do this too quickly. Check the requirements and the hierarchy diagram.