What is the output of the following application?
package mypkg; /* w ww . j a v a 2 s .c o m*/ interface Box { void print(); default int getVolume() {return 5;} } interface Cube { void print(); default int getVolume() {return 10;} } class Block implements Box, Cube { @Override public void print() {System.out.print("A");} } public class Main { public static void main(String[] strings) { final class MyBlock extends Block { public void print() {System.out.print("E");} } } }
D.
The application does not compile, so Options A, B, and C are incorrect.
The one and only compilation issue is that the Block class implements two interfaces that declare default methods with the same getVolume()
signature.
A class can inherit two default methods with the same signature but only if the class overrides the methods with its own version.
Since Block does not override the method, the Block class does not compile.
Since this is the only compilation issue, Option D is the correct answer.
If the Block class did correctly override the getVolume()
method, the rest of the code would compile without issue.
In this case, there would be nothing printed at runtime.
The main()
method just declares a local inner class but does not create an instance of it, nor does it call any method on it, making Option C the correct answer.