Is this code correct?
YouthBirthday birth; birth = new Birthday( "Terry", 23 );
No. A reference variable for a child class (YouthBirthday) cannot be used for an object of a parent class (Birthday).
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
:
greeting()
method.
Here is a skeleton:
Fill in the missing parts.