224k views
0 votes
33) What is printed?

public class Inherit
{
abstract class Figure
{
void display( )
{
System.out.println("Figure");
}
}
class Line extends Figure
{
void display( )
{
System.out.println("Line");
}
}
void tryme(Figure f)
{
f.display( );
}
Inherit( )
{
Figure f = new Line( );
tryme(f);
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
a) Line
b) Figure
c) Shape
d) Syntax error; the code won't even compile
e) None of the above

User Mkind
by
8.9k points

1 Answer

3 votes

Final answer:

Executing the code snippet will print "Line" to the console because the actual object passed to the tryme method is of type Line, whose display method is invoked.

Step-by-step explanation:

The question asks what is printed when the code snippet is executed. The code defines an abstract class Figure with a display method, which is then overridden in the subclass Line. The constructor of the Inherit class creates an instance of Line and passes it to the tryme method. In Java, method invocation is determined at runtime based on the actual object's type. Since the actual object is of type Line, the overridden display method in Line will be called, and "Line" will be printed to the console.

User Brian Hoover
by
8.3k points