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

It is a common practice to begin a procedure's name with a(n) ____.

A. noun B. verb C. adjective D. adverb

Computer Science & Information Technology

Which of the following is an attribute of a network device built into the network interface?

A. serial number B. MAC address C. IP address D. model number

Computer Science & Information Technology