Which option enables the code to compile?

And given the code fragment:
Book book1 = new EBook ();
Which option enables the code to compile?

A. Option A
B. Option B
C. Option C
D. Option D

Download Printable PDF. VALID exam to help you PASS.

13 thoughts on “Which option enables the code to compile?

  1. Yep D.
    – I fell victim to not considering the actual program which instantiates EBook, so Ebook cannot be abstract if we wanna compile the code at some point

  2. only D will allow to compile it without error:
    # javac Test28.java

    A is NOT correct , it will not allow to compile:
    # javac Test28.java
    Test28.java:18: error: EBook is abstract; cannot be instantiated
    Book book1 = new EBook();
    1 error

  3. Answer D:

    The main problem here is that the method setBookmark() of the interface has never been implemented by any of the descendant classes. That can be resolved by either making all the descendant classes abstract, or to implement the setBookmar() method at the first concrete class.

    In this example, the first concrete class is EBook. So implementing the setBookmark() here solves the issue. Accordingly, Answer “D” is the correct one.

    1. What is confusing though is the following line:
      Book book1 = new EBook();

      It is misleading because it looks like there is an attempt to instantiate the abstract class “Book”. But this is not true. While the new reference variable “book1” is of type Book, it is pointing to an object of type “EBook” actually. Which means that this line instantiate a normal non-abstract class, which is legal. So option “B” is not correct.
      For the same reason, answer “A” is not correct, because if you marked EBook as abstract, then you cannot instantiate it.

      Lastly, option “C” is not correct because you cannot use abstract methods in non-abstract classes.

  4. answer is D
    interface Readable{
    public void readBook();
    public void setBookMark();

    abstract class Book implements Readable{
    public void readBook(){}

    public class EBook extends Book{
    public void readBook(){}
    public void setBookMark(){}

    public static void main(String[] args) {
    Book book1=new EBook();

  5. C

    Option A will result “Book book1 = new EBook ();” compile fails, because abstract can’t be an new instance.

  6. Answer is D. Abstract class cannot be instantiated. Just override setBookMark in class EBook and the code will compile.

  7. A and D answer. If you make the class abstract no need to implement all methods from the interface. Otherwise, you need to implement all the methods from the interface.

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.