What is the output of the following code? (Choose all that apply)
1: interface Area { 2: public default int getArea(int input) { return 2; } 3: } //from w w w . j ava2 s . c o m 4: public class Square implements Area { 5: public String getArea() { return "4"; } 6: public String getArea(int input) { return "6"; } 7: public static void main(String[] args) { 8: System.out.println(new Square().getArea(-1)); 9: } 10: }
E.
The code doesn't compile because line 6 contains an incompatible override of the getArea
(int input) method defined in the Area interface.
In particular, int and String are not covariant returns types, since int is not a subclass of String.
Note that line 5 compiles without issue; getArea()
is an overloaded method that is not related to the parent interface method that takes an int value.