1Z0-809 practice question 22 of 25

Given: Book.java: public class Book { private String read(String bname) { return “Read” + bname } } EBook.java: public class EBook extends Book {…

Full set: 196 questions
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 ProgrammingView http:/ ebook.com/ebook
B: Read Java ProgrammingRead http:/ ebook.com/ebook
C: The EBook.java file fails to compile.
D: The Test.java file fails to compile. Correct answer
Correct Answer:
D. The Test.java file fails to compile.
Explanation

Book.read(String) is declared private, so it is not visible outside Book. The call b1.read(...) in Test is therefore illegal and the compilation of Test.java fails. Subclass EBook’s public read method is irrelevant because b1 is a Book reference and the inherited private method is invisible.

Why Incorrect

A – Would require successful compilation and accessible read; fails to compile. B – Same accessibility problem; cannot compile. C – EBook compiles: widening access from private (not inherited) to public is legal (§6.6).

References

1. Java Language Specification (SE 8) §6.6.1, “Determining Accessibility”.

2. JLS (SE 8) §8.4.8.3, “Overriding (by Instance Methods)”, note on private methods not inherited.

3. JLS (SE 8) §7.2, “Compilation Units”, access errors are compile-time errors.

Scroll to Top