1Z0-819 · Question #189
``java public class X { protected void print(Object obj) { print(objects); } public final void print(Object... objects) { collection.forEach(System.out::println); } } public class Y extends X {…
The correct answer is D. The method print (Object) and the method print (Object...) are duplicates of each other. The method X.print (Object...) cannot override the final method X.print (Object...). Option D is correct because Y.print(Object... objects) attempts to override X.print(Object... objects), but that method is declared final in class X - Java prohibits overriding final methods, so the compiler rejects class Y entirely. The first half of option D ("duplicates of…
Question
public class X {
protected void print(Object obj) {
print(objects);
}
public final void print(Object... objects) {
collection.forEach(System.out::println);
}
}
public class Y extends X {
public void print(Object obj) {
System.out.println("obj " + obj);
}
public void print(Object... objects) {
for (Object object : objects) {
System.out.println("[" + object + "]");
}
}
public void print(Collection collection) {
print(collection.toArray());
}
}
Why does this compilation fail?Options
- AThe method X.print(Object) does not call the method super.print(Object)
- BThe method X.print(Object...) is not accessible to Y
- CThe method Y.print (collection), however. Out of scope, is an invalid Java identifier.
- DThe method print (Object) and the method print (Object...) are duplicates of each other. The method X.print (Object...) cannot override the final method X.print (Object...)
How the community answered
(16 responses)- A13% (2)
- B6% (1)
- C6% (1)
- D75% (12)
Explanation
Option D is correct because Y.print(Object... objects) attempts to override X.print(Object... objects), but that method is declared final in class X - Java prohibits overriding final methods, so the compiler rejects class Y entirely. The first half of option D ("duplicates of each other") is a red herring phrasing; the decisive issue is the final violation, not duplication.
Why the distractors are wrong:
- A is wrong because there is no rule requiring a method to call
super; omitting asupercall is valid and not a compilation error. - B is wrong because
X.print(Object...)is declaredpublic, making it fully accessible to any subclass including Y. - C is wrong because
collectionis a perfectly valid Java identifier; the sentence itself is grammatically nonsensical and describes no real Java rule.
Memory tip: Think of final on a method as "the last word" - no subclass can have the final word on that method. Whenever you see a subclass redefine a method that a parent marked final, that's an instant compile error, regardless of access modifiers or parameter types.
Community Discussion
No community discussion yet for this question.