What is the result?

Given the code fragments:

What is the result?
A. Compilation fails only at line n1.
B. Compilation fails only at line n2.
C. Tool::export
Tool::export
D. Compilation fails at both line n1 and line2.
E. RTool::export Tool::export

Download Printable PDF. VALID exam to help you PASS.

7 thoughts on “What is the result?

  1. Complie Time Error :
    Multiple markers at this line
    – Cannot reduce the visibility of the inherited method
    from Tool
    – overrides com.vce.Tool.export

    Answer B

  2. -interface methods default to public
    -interface methods can never be protected or private in java 8
    -This fails to compile because we’re attempting to hide the super class’ export() method but can’t unless we grant equal or more access the our new export() method. Would work fine if our new export() was public however never do “method hiding” in the real world because you’d get fired on the spot.

  3. B:

    # javac ReportTool.java
    ReportTool.java:12: error: export() in ReportTool cannot implement export() in E xportable
    void export() {
    ^
    attempting to assign weaker access privileges; was public
    1 error

  4. Answer B.
    To override a method, the overriding method should not have weaker access privileges. In line n2, this overriding method has no access modifier, which is interpreted as “default” access, meaning that this method is accessible from classes in the current package only. And this is less accessible than the original overridden export () method in ReportTool direct parent class (i.e. the Tool class), which was a public method.
    To fix the problem, add “public” access modifier to the export() of ReportTool class.

Leave a Reply

Your email address will not be published.