16.5k views
0 votes
21) "class Aggregate" is incorrect. Choose the correct line so that this program prints:

Granite: weight=25.0 value=4 numKind=7
public class Inherit
{
abstract class Stone
{
protected float weight = 13;
protected int value = 4;
abstract public String toString( );
}
class Aggregate
{
protected int numKind;
}
class Granite extends Aggregate
{
Granite( )
{
weight = 25; numKind = 7;
}
public String toString( )
{
return "Granite: weight="
+ weight + " value="
+ value + " numKind="
+ numKind;
}
}
Inherit( )
{
Granite g = new Granite( );
System.out.println(g);
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
a) abstract class Aggregate {
b) abstract class Aggregate extends Granite {
c) abstract class Aggregate extends Stone {
d) class Aggregate extends Stone {
e) None of the above

1 Answer

4 votes

Final answer:

The correct line to replace "class Aggregate" with is "class Aggregate extends Stone {" because it enables Granite to inherit properties from Stone through Aggregate.

Step-by-step explanation:

To correct the line "class Aggregate" in the given Java program so that it prints "Granite: weight=25.0 value=4 numKind=7" upon execution, we need to ensure that the Granite class can inherit the properties weight and value. In the provided class hierarchy, Stone contains these properties. Therefore, for Granite to correctly inherit these properties through Aggregate, Aggregate should extend Stone. The correct line of code to replace "class Aggregate" would be:

class Aggregate extends Stone {

This allows the Granite class to inherit from Aggregate, and therefore indirectly from Stone, gaining access to the weight and value fields. As a result, the class structure becomes more vivid and the object of Granite can correctly explain its state through its toString() method.

User Puyol
by
8.5k points