82.5k views
1 vote
Given the following partial class definitions: public class Book implements Comparable { // code for class public class}public class Dictionary extends Book { // code for class }Which declaration will result in a compiler error? A. Comparable c = new Dictionary(): B. Book b = new Book(): C. Book b = new Dictionary ():D. Comparable c = new Book();E. Dictionary d = new Book();

1 Answer

3 votes

Answer:

Step-by-step explanation:

The declaration that will result in a compiler error is E. Dictionary d = new Book();

This is because you cannot assign a superclass object to a subclass reference variable. The class Book is the superclass and Dictionary is the subclass, so it is not allowed to assign a Book object to a Dictionary reference variable.

Option A is valid because a Dictionary object can be assigned to a Comparable reference variable since Dictionary implements the Comparable interface.

Option B is valid because it declares a Book object and initializes it properly.

Option C is valid because a Dictionary object can be assigned to a Book reference variable since Dictionary is a subclass of Book.

Option D is valid because a Book object can be assigned to a Comparable reference variable since Book implements the Comparable interface.

User Rodrigo Oliveira
by
8.1k points