Java homework on Polymorphism~ looking for help~ What is printed by the followin
ID: 3839340 • Letter: J
Question
Java homework on Polymorphism~ looking for help~
What is printed by the following code?
public class Figure {
public void display() {
System.out.print("Figure");
}
} public class Rectangle extends Figure {
public void display() {
super.display();
System.out.print(“Rectangle”);
}
}
public class Box extends Rectangle {
public void display() {
System.out.print("Box");
}
} public class DoIt {
public static void main(String[ ] args) {
Figure f = new Rectangle();
f.display();
Figure f2 = new Box();
f2.display();
}
}
Answer:
Question 18
In the question above, what is the declared type of f2?
Select one:
Box
None of the above
Rectangle
Figure
Question 19
In the question above, what is the actual type of f2?
Select one:
None of the above
Rectangle
Box
Figure
Question 20
What is printed by the following code?
public class Figure {
public void display() {
System.out.print("Figure");
}
} public class Rectangle extends Figure {
public void display() {
System.out.print(“Rectangle”);
}
}
public class Box extends Rectangle {
public void display( ) {
System.out.print("Box");
}
} public class DoIt {
public static void main(String[ ] args) {
Figure f = new Figure();
f.display();
Figure f2 = new Rectangle();
f2.display();
Figure f3 = new Box();
f3.display();
}
}
Answer:
Question 21
What is printed by the following code?
public class Figure {
public void display() {
System.out.print("Figure");
}
public void display(String s) {
System.out.print("Figure" + s);
}
} public class Rectangle extends Figure {
public void display() {
System.out.print(“Rectangle”);
}
public void display(String s) {
System.out.print("Rectangle" + s);
}
}
public class Box extends Rectangle {
public void display( ) {
System.out.print("Box");
}
public void display(String s) {
System.out.print("Box" + s);
}
} public class DoIt {
public static void main(String[ ] args) {
Figure f = new Figure();
f.display();
Figure f2 = new Rectangle();
f2.display("One");
Figure f3 = new Box();
f3.display("Two");
}
}
Answer:
Explanation / Answer
The output is as follows:
FigureRectangleBox
18
figure
19
rectangle
20 The output is as follows:
FigureRectangleBox
21
FigureRectangleOneBoxTwo
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.