Refer to the following classes. public abstract class Alpha { private int value
ID: 3741193 • Letter: R
Question
Refer to the following classes.
public abstract class Alpha
{
private int value = 4;
public Alpha( )
{ }
public abstract String display( );
public void setValue( )
{ value = n; }
public int getValue( )
{ return value; }
}
public class Beta extends Alpha
{
private int num;
public Beta(int myNum)
{
super( );
num = myNum;
super.setValue(myNum - 3);
}
public String display( )
{
return "value: " + getValue( ) + " num: " + num;
}
}
public class Gamma extends Alpha
{
private int num;
public Gamma(int myNum)
{
super( );
num = myNum;
}
public String display( )
{
return "num: " + num + " value: " + getValue( );
}
}
Consider the following code segment in the main method of another class.
Alpha bObject = new Beta(5);
Alpha gObject = new Gamma(6);
System.out.println(bObject.display( ));
System.out.println(gObject.display( ));
Which is printed as a result of executing the code segment?
Question 1 options:
value: 2 num: 5
value: 2 num: 5
value: 2 num: 5
num: 6 value: 3
value: 2 num: 5
value: 3 num: 6
value: 2 num: 5
value: 4 num: 6
value: 2 num: 5
num: 6 value: 4
1value: 2 num: 5
value: 2 num: 5
2value: 2 num: 5
num: 6 value: 3
3value: 2 num: 5
value: 3 num: 6
4value: 2 num: 5
value: 4 num: 6
5value: 2 num: 5
num: 6 value: 4
Explanation / Answer
The class Alpha should be edited to run the code.
Its function setValue() should have a parameter as follows
public void setValue(int n)
{ value = n; }
The expected result will be oprtion 5
Explanation
Alpha bObject = new Beta(5);
This will set value = 2 using the setValue of super class Alpha
num will be 5 using constructor of Beta.
Alpha gObject = new Gamma(6);
num=6 using constructor of Gamma.value=6 as the super class constructor is called.