75.8k views
4 votes
What is printed by the following code? Consider the polymorphic invocation.

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
e) None of the above"

User Unniverzal
by
9.1k points

1 Answer

2 votes

Final answer:

The code will print 'Figure', 'Rectangle', and 'Box'.

Step-by-step explanation:

The code will print:

  1. Figure
  2. Rectangle
  3. Box

The code defines three classes: Figure, Rectangle, and Box. Figure is the parent class, while Rectangle and Box are its subclasses. The code creates objects of each class and invokes the display() method on those objects. When the display() method is called on the object of the Figure class, it prints 'Figure'. When the same method is called on the object of the Rectangle class, it prints 'Rectangle', and when it is called on the object of the Box class, it prints 'Box'.

User Exploitr
by
8.6k points