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

Is this code correct?

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

Answer:

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


New Definition of YouthBirthday

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.
  1. A method with the name of the sender as a parameter.
    • This method's signature is different from the parent's.
    • 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 writes out How you have grown!! Love, followed by the name of the sender.

// 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.


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