1Z0-809 · Question #119
Given: package p1; public interface DoInterface { void m1(int n); public void m2(int n); } package p3; import p1.DoInterface; public class DoClass implements DoInterface { int x1, x2; DoClass() {…
The correct answer is D. Compilation fails due to an error at line n3. Option D is correct because the Test class calls doi.method1(100) and doi.method2(200), but DoInterface only declares m1 and m2 - the names method1 and method2 simply don't exist. Since doi is statically typed as DoInterface, the compiler resolves method calls against the…
Question
Options
- A100
- BCompilation fails due to an error in line n1
- CCompilation fails due to an error at line n2
- DCompilation fails due to an error at line n3
How the community answered
(46 responses)- A7% (3)
- B13% (6)
- C4% (2)
- D76% (35)
Explanation
Option D is correct because the Test class calls doi.method1(100) and doi.method2(200), but DoInterface only declares m1 and m2 - the names method1 and method2 simply don't exist. Since doi is statically typed as DoInterface, the compiler resolves method calls against the interface contract, finds no match, and fails inside the main method block (line n3).
Why the distractors are wrong:
- A (100): The code never compiles, so no output is produced at all.
- B (line n1): The interface is valid. In Java interfaces, all methods are implicitly
public abstract, so mixingvoid m1(int n)(implicit public) andpublic void m2(int n)(explicit public) is perfectly legal - no error there. - C (line n2):
DoClass.m1is a valid, properlypublicimplementation of the interface'sm1method; the signature matches exactly and causes no compilation issue.
Memory tip: Think of an interface reference as a locked contract - the compiler only knows what's written in the interface, not what the concrete class can do. If the method name isn't in the interface, the call won't compile, regardless of what the implementing class provides. Always verify method names against the interface declaration, not the class.
Community Discussion
No community discussion yet for this question.