208k views
4 votes
24) What is printed?

public class Inherit
{
class Figure
{
void display( )
{
System.out.println("Figure");
}
}
class Rectangle extends Figure
{
void display( )
{
System.out.println("Rectangle");
}
}
class Box extends Figure
{
void display( )
{
System.out.println("Box");
}
}
Inherit( )
{
Figure f = new Figure( );
Rectangle r = new Rectangle( );
Box b = new Box( );
f.display( );
f = r;
f.display( );
f = b;
f.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
a) Figure
Rectangle
Box
b) Rectangle
Box
c) Figure
Figure
Figure
d) Syntax error; this code won't compile or execute

1 Answer

0 votes

Final answer:

The code provided creates instances of nested classes and calls their respective display methods to print specific messages.

Step-by-step explanation:

The code provided defines a class Inherit with three nested classes: Figure, Rectangle, and Box. Each class contains a method called display() that prints out a specific message.

In the main method, an instance of each class is created: f represents a Figure, r represents a Rectangle, and b represents a Box. The display() method is called on each instance, resulting in the output: Figure, Rectangle, and Box.

The correct answer is Option (a) FigureRectangleBox.

User Ariddell
by
7.5k points