Object obj; YouthBirthday ybd = new YouthBirthday( "Ian", 4 ); String str = "Yertle"; obj = ybd; if ( obj instanceof Card ) // Note changes here ((Card)obj).greeting(); else if ( obj instanceof String ) System.out.print( (String)obj );
Yes. After the fourth statement, obj points to a YouthBirthday object
(that is a grandchild of Card) so instanceof returns true.
The greeting() method of the Card object will run.
You have reached the end of the chapter. You may wish to review the following. Click on a subject that interests you to go to an instance of its discussion.
super in a child method.
instanceof operator.
The next chapter will discuss interfaces— another object oriented aspect
of Java.