What is the output of the following application?
package mypkg;// w w w. j a va 2s. com abstract interface Printable { public void print(); } public class Main { public static void main(String[] seaweed) { int distance = 7; Printable p = { @Override public void print() { System.out.print(distance); } }; p.print(); } }
C.
The main()
method attempts to define an anonymous inner class instance but fails to provide the class or interface name, or use the new keyword.
The right-hand side of the assignment to the p variable should start with new Printable()
.
For this reason, Option C is the correct answer.
If the code was corrected with the proper declaration, it would output 7, and Option B would be the correct answer.