What is the result?
A. Compilation fails only at line n2.
B. RTool::export
Tool::export
C. Tool::export
Tool:export
D. Compilation fails only at line n1.
E. Compilation fails at both line n1 and line n2.
What is the result?
A. Compilation fails only at line n2.
B. RTool::export
Tool::export
C. Tool::export
Tool:export
D. Compilation fails only at line n1.
E. Compilation fails at both line n1 and line n2.
D
Correct is B
The given code snippet has incorrect keyword ‘Interface’ i.e upper case ‘I’ instead of ‘i’
IDE gives a compilation error.
D is correct : Cannot reduce the visibility of the inherited method from Downloadable
There is not problem with Line N2
class Tool implements Exportable {
protected void export() { // replace with public will work
}
}
public class ReportTool extends Tool implements Exportable {
public void export() {
}
public static void main(String[] args) {
Tool aToll = new ReportTool();
Tool btool = new Tool();
callExport(aToll);
callExport(btool);
}
public static void callExport(Exportable ex) {
ex.export();
}
}
interface Exportable {
void export();
}
D
RTool::export
Tool::export
Option : B
D :
# javac ReportTool.java
ReportTool.java:6: error: export() in Tool cannot implement export() in Exportable
protected void export () {
^
attempting to assign weaker access privileges; was public
1 error
if in “n1” position “protected” replace by “public” , this program will be compiled with no errors, with following result:
# java ReportTool
RTool::export
Tool::export
Answer D:
Because the signature of the implementing methods in the first concrete class must match the signature of the abstract methods of the interface.
In this example, the access modifier in Tool is protected, while in the interface it is default (i.e. no access modifier). Therefore the compilation fails.
in the interface, the method is implicitly public
All interface methods are public by default. The problem is that we’re providing weaker access by making it protected
Answer is D. Just try it in your IDE.
D
Answer is D.
B
class Tool implements Exportable{
public void export() {
System.out.println(“Tool::export”);
}
}
public class ReportTool extends Tool implements Exportable{
public void export() {
System.out.println(“RTool::export”);
}
public static void main(String[] args) {
Tool aToll = new ReportTool();
Tool btool = new Tool();
callExport(aToll);
callExport(btool);
}
public static void callExport(Exportable ex) {
ex.export();
}
}
interface Exportable{
void export();
}
Giving me : answer B >>
$javac ReportTool.java
$java -Xmx128M -Xms16M ReportTool
RTool::export
Tool::export
you missed this
class Tool implements Exportable{
public void export() {
System.out.println(“Tool::export”);
}
}
||
V
class Tool implements Exportable{
protected void export() {
System.out.println(“Tool::export”);
}
}
answer is D
d
B