asked 97.3k views
1 vote
Which of the following best explains why the code segment will not complie. A. Line 2 will not compile because variables of type Book may not refer to variables of type AudioBook.

B. Line 4 will not compile because variables of type Book may only call methods in the Book class.
C. Line 5 will not compile because the AudioBook class does not have a method named toString declared or implemented.
D. Line 6 will not compile because the statement is ambiguous. The compiler cannot determine which length method should be called.
E .Line 7 will not compile because the element at index 1 in the array named books may not have been initialized.

asked
User Pitfall
by
8.5k points

1 Answer

4 votes
A. Line 2 will not compile because variables of type Book may not refer to variables of type AudioBook.

This is because AudioBook is a subclass of Book, and the reference variable book1 is being used to refer to an object of type AudioBook. So, this is not an error and will compile successfully.

B. Line 4 will not compile because variables of type Book may only call methods in the Book class.

This is not correct since book1 is declared as a variable of type AudioBook, which is a subclass of Book, and it can call methods of its super class.

C. Line 5 will not compile because the AudioBook class does not have a method named toString declared or implemented.

This is also incorrect as AudioBook inherits the toString() method from its superclass, Book.

D. Line 6 will not compile because the statement is ambiguous. The compiler cannot determine which length method should be called.

This is not related to the code segment provided, so it is not the correct answer.

E. Line 7 will not compile because the element at index 1 in the array named books may not have been initialized.

This is not correct as Java initializes arrays with default values, so the element at index 1 will already have a default value assigned to it.

Therefore, the correct answer is (A) Line 2 will not compile because variables of type Book may not refer to variables of type AudioBook.
answered
User WillDonohoe
by
7.5k points