An array can be created to reserve space for references to objects. Using the code above, explain how an array of superclass references can hold subclass references.

What will be an ideal response?


The Animal[] animalRef = new Animal[3]; statement reserves enough computer memory for three Animal objects named animalRef[0], animalRef[1], and animalRef[2]. The statement does not actually instantiate Animals; Animal is an abstract class and cannot be instantiated. The Animal array declaration simply reserves memory for three object references. If you instantiate objects from Animal subclasses, you can place references to those objects in the Animal array. The array of three references is used to access each appropriate speak() method.

In the AnimalArrayDemo application, a reference to an instance of the Dog class is assigned to the first Animal reference, and then references to Cow and Snake objects are assigned to the second and third array elements. After the object references are in the array, you can manipulate them like any other array elements. The application uses a for loop and a subscript to get each individual reference to speak().

Computer Science & Information Technology

You might also like to view...

Given that the variable days has a value of 14, the following expression would evaluate to what value? days / 7 + 1

a. 1.75 b. 2 c. 3 d. 4 e. None of these

Computer Science & Information Technology

When a website operator states in its privacy notice that it will not share financial information with third parties and then shares financial information with a third-party affiliate, what recourse may occur?

A. The FTC may bring an action against the operator for unfair competition B. The FTC may bring an action against the operator for a deceptive trade practice C. A user of the website may bring a criminal complaint against the operator D. The FTC may bring an action under Section 7 of the FTC Act

Computer Science & Information Technology

Which of the following is NOT one of the Google+ default circles?

A) Friends B) Colleagues C) Family D) Following

Computer Science & Information Technology

Off-slide content includes any objects saved with the presentation that do not appear on a slide

Indicate whether the statement is true or false

Computer Science & Information Technology