Java SE 8 Programmer II — Question 9
Given:
Book.java:
public class Book {
private String read(String bname) { return "Read" + bname }
}
EBook.java:
public class EBook extends Book {
public class String read (String url) { return "View" + url }
}
Test.java:
public class Test {
public static void main (String[] args) {
Book b1 = new Book();
b1.read("Java Programing");
Book b2 = new EBook();
b2.read("http://ebook.com/ebook");
}
}
What is the result?
Answer options
- A. Read Java Programming View http:/ ebook.com/ebook
- B. Read Java Programming Read http:/ ebook.com/ebook
- C. The EBook.java file fails to compile.
- D. The Test.java file fails to compile.
Correct answer: D
Explanation
The Test.java file fails to compile because the method signature in EBook.java is incorrect; it redefines the read method in a way that does not match the parent class's method signature. Therefore, the second call to b2.read() will cause a compilation error, making option D the correct answer while the other options describe results that would not occur.