What is the output of the following code?
1: class Shape 2: public void printName(double input) { System.out.print("Shape"); } 3: } 4: public class Rectangle extends Shape { 5: public void printName(int input) { System.out.print("Rectangle"); } 6: public static void main(String[] args) { 7: Rectangle r = new Rectangle(); 8: r.printName(4); 9: r.printName(9.0); 10: } 11: }
A.
The code compiles and runs without issue, so E and F are incorrect.
The printName() method is an overload in Rectangle, not an override, so both methods may be called.
The call on line 8 references the version that takes an int as input defined in the Rectangle class, and the call on line 9 references the version in the Shape class that takes a double.
RectangleShape is output and option A is the correct answer.