Given the code fragments:
What is the result?
Given the code fragments:
What is the result?
The problem in the code lies in the access modifier used for the `export` method in the `Tool` class. In Java, methods in an interface are implicitly public. When a class implements an interface, the methods must be at least as accessible as they are in the interface. However, in the `Tool` class, the `export` method is declared with protected access, which is more restrictive than public. This is not allowed and will result in a compilation error at line n1. Therefore, the correct answer is that compilation fails only at line n1.
D. By default, method declaration in an interface is implicitly Public abstract. Overiding with protected is a more restrictive access modifier.
Guys, in my case, testing here at bluej, I verified that the problem is in the interface, marking the Void with a capital v, in this case, the compilation of line n1 and n2 fails. So what convinced me the most was the answer "E".
Void (uppercase): It is a class in Java that is part of the java.lang package. It is used as a generic type to indicate that a generic method doesn't return any value. It is typically used in contexts where generic types are needed, such as in Callable<Void> or in generic classes that can work with different data types, including Void. It should not be confused with the use of void in method signatures.
Answer is D, tested.
Answer is D. "Tool::export" is printed and java.lang.IllegalAccessError error is thrown. Although there is a print on the terminal, this error is classified as a compilation error, according to oracle documentation. https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalAccessError.html To test: interface Exportable { void export(); } class Tool implements Exportable { protected void export () { // line n1 - throwIllegalAccessError System.out.println("Tool::export"); } } class ReportTool extends Tool implements Exportable { public void export () { // line n2 System.out.println("RTool::export"); } public static void main(String[] args) { Tool aTool = new ReportTool(); Tool bTool = new Tool(); callExport(aTool); callExport(bTool); } public static void callExport (Exportable ex) { ex.export(); } }
Correct Answer is D. Reason: "Cannot reduce the visibility of the inherited method from Exportable interface"
All abstract, default, and static methods in an interface are implicitly public. void export() has not a package-private access. It is implicitly public. Answer is D. The other typo are not delibarely generated I think.
ANS: D The implicitly default access modifier in the interface can only be public, thus couldn't overrding with the protected access modifer
Answer - D Access modifier of the implementation method in child class either has to be same or higher. In this scenario public. NOT protected. public void export(){ System.out.println("Tool: Export"); }
agreed D tested
Compilaion error on Interface. Syntax error on Void
Agreed, answer is D