?
The AnimalReference application shown above contains a generic Animal reference variable into which you can assign concrete Animal objects. Explain how this example uses polymorphism and why it is important.

What will be an ideal response?


If you create an Animal class and various subclasses, such as Dog, Cow, and Snake, you can create an application containing a generic Animal reference variable into which you can assign any of the concreteAnimalchild objects. The variableanimalRefis a type ofAnimal. No superclassAnimalobject is created (none can be); instead,DogandCowobjects are created using thenewkeyword. When theCowobject is assigned to theAnimalreference, theanimalRef.speak() method call results in "Moo!"; when theDogobject is assigned to theAnimalreference, the method call results in "Woof!" Assigning a variable or constant of one type to a variable of another type is called promotion, implicit conversion, or upcasting.?The animalRef application shows that using a reference polymorphically allows you to extend a base class and use extended objects when a base class type is expected. For example, you could pass aDogor aCowto a method that expects anAnimal. This means that all methods written to accept a superclass argument can also be used with its children-a feature that saves child-class creators a lot of work.

Computer Science & Information Technology

You might also like to view...

______ are used to document a program and improve its readability.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

________ features a hotlist of the most popular bookmarked pages and articles on its home page and offers RSS feeds of bookmarks by tag

A) Digg B) reddit C) Delicious D) StumbleUpon

Computer Science & Information Technology

Which chart type is preferable when the category names are long?

A) Bar B) Pie C) Stacked column D) Column

Computer Science & Information Technology

Text-only links help search engines to find all the pages on your Web site so they show up in search results.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology