What is the output of the following code?
1: interface Printable { 2: default boolean isPrinted() { return true; } 3: } 4: public class Shape implements Printable { 5: public boolean isPrinted() { return false; } 6: public static void main(String[] args) { 7: Printable p = (Printable)new Shape(); 8: System.out.println(p.isPrinted()); 9: } 10: }
B.
This code compiles and runs without issue, outputting false.
The first declaration of isPrinted() is as a default interface method, assumed public.
The second declaration of isPrinted() correctly overrides the default interface method.
Shape instance may be automatically cast to a Printable reference without an explicit cast, although adding it doesn't break the code.