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 questionsBook.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.
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).
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.
Keep practicing
Take the free 1Z0-809 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 1Z0-809 practice questions
- Question 2Given: class Student { String course, name, city; public Student (String name, String cou…
- Question 3Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { …
- Question 4Given the code fragment: Path file = Paths.get (“courses.txt”); // line n1 Assume the cou…
- Question 5Given that /green.txt and /colors/yellow.txt are accessible, and the code fragment: Path …
- Question 6Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Ma…
- Question 7Given the code fragment: List str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”); Pre…
